in reply to Catch loss of communication

If select doesn't help, you could periodically "ping" the connection by issuing a command that does nothing.