Socket.Connected
represents the state of the socket at the last read or write operation, not the current state of the socket. There are several different ways to handle disconnects:
1) When the client disconnects it can send a message to the server saying that it's about to disconnect.
2) Have the server use the
Socket.Poll[
^] method to get the current status. The return value from that can be combined with other information to determine if the socket is still open.
3) Have the server periodically send out a heartbeat message to the clients to test the connection. If it's closed then the send will fail.
Using (1) will give you instantaneous notification when a client drops and (3) will handle clients that improperly drop their connections. (3) will also keep the client aware of the socket state since it can check for the heartbeats. (2) is similar to (3) except that nothing is sent to the clients so both client and server would have to actively check the socket state.