What I usually do is use a read timeout on the client to initiate a server poll, then go back to reading. If another read timeout occurs, the client knows that its server is unreachable, closes its socket and falls back to attempting to re-open the connection every, say, 10 seconds. Pseudo-C:
boolean pollOustanding;
while true{
if connectAttempt(){
pollOutstanding:=false;
while(true){
if readWithTimeout(2000){
handleReceivedData(); // could be 'real' data, or poll reply from server
pollOutstanding:=false;
}
else
{ if (pollOutstanding) break;
pollOutstanding=true;
sendPoll();
};
};
closeSocket;
};
Sleep(10000);
};
Only needs one thread - you don't need to poll the sever if data is being received - the connection must be OK.