title: C# detect TCP client disconnection
body: <p>I am working on TCP multithread server with C# window application form, and I am trying to detect if the machine of client is shutdown and disconnects from the server. I have read some posts and have some ideas:</p>
  
  <p><a href="https://stackoverflow.com/questions/6993295/how-to-determine-if-the-tcp-is-connected-or-not">How to determine if the tcp is connected or not?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket">Instantly detect client disconnection from server socket</a></p>
  
  <p>But I'm not sure where to call the function <code>IsConnected</code></p>
  
  <p>My code is like the following:</p>
  
  <pre><code>public BindingList&lt;Tablet&gt; tabletList = new BindingList&lt;Tablet&gt;();
  private Socket socket_Server = null;
      private Thread myThread = null;
      private Socket socket_Connect = null;
      private Dictionary&lt;string, Socket&gt; dic = new Dictionary&lt;string, Socket&gt; { };
      private string RemoteEndPoint;
  
  socket_Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          IPAddress ServerIP = IPAddress.Parse("192.168.2.146");
          IPEndPoint point = new IPEndPoint(ServerIP, portNum);
          socket_Server.Bind(point);
          socket_Server.Listen(50);
          myThread = new Thread(Listen_Disp);
          myThread.IsBackground = true;
          myThread.Start();
          Console.WriteLine("Server start");
  
  private void Listen_Disp()
      {
          try
          {
              while (true)
              {
  
                  //This is not working
                  for (int i = 0; i &lt; tabletList.Count; i++)
                  {
                      if (!SocketConnected(dic[tabletList[i].ip]))
                      {
                          Console.WriteLine(RemoteEndPoint + "disconnected");
                      }
                  }
  
                  try
                  {
                      socket_Connect = socket_Server.Accept();
                      RemoteEndPoint = socket_Connect.RemoteEndPoint.ToString();
                      Console.WriteLine(RemoteEndPoint + " is connected");
                      dic.Add(RemoteEndPoint, socket_Connect);
  
                      Tablet newTablet = new Tablet();
                      newTablet.ip = RemoteEndPoint;
                      newTablet.status = "Online";
                      tabletList.Add(newTablet);
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.ToString());
                  }
              }
  
              Console.WriteLine("end of while");
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.ToString());
          }
      }
  
  static class SocketExtensions
  {
      public static bool IsConnected(this Socket socket)
      {
          try
          {
              return !(socket.Poll(1, SelectMode.SelectRead) &amp;&amp; socket.Available == 0);
          }
          catch (SocketException) { return false; }
      }
  }
  </code></pre>
  
  <p>Thanks for help.</p>