title: How to detect disconnected state of TCP Socket
body: <p>I’m currently working on a project in which I need to receive data from devices over GPRS. These devices send their data to a TCP server listening on a port on my server. I first had the problem with connecting multiple devices and after some research I learned that I should implement an Asynchronous approach. I now have 1 TCP socket listening for incoming connections and as soon as a request comes in this is put in a new socket connection, freeing the listening socket for other connection attempts.</p>
  
  <p>My challenge: I am testing with 2 devices, these connect successfully and can send their data. When I turn off a device (remove battery and SIM) I am not able to detect that this device is no longer connected and close the socket. When running a netstat on the port of the listener I still see 2 devices with connection ‘ESTABLISHED’. </p>
  
  <p><a href="https://i.stack.imgur.com/4OaZr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4OaZr.png" alt=""></a></p>
  
  <p>I have tried various examples but the various socket polling options all return ‘true’. The only option that works now is trying to send some data to the socket which will fail and gets catched and then return a false value. Setting the ReceiveTimeout property on the socket also does not help.
  Thing I tried:</p>
  
  <pre><code>socket.Poll(1000, SelectMode.SelectWrite); //returns true
  socket.Poll(1000, SelectMode.SelectRead); //returns true
  socket.Available = 0 (available);
  </code></pre>