title: How to determine if the tcp is connected or not?
body: <p>I have tcpclient object and i want to determine if it's connected or not.
  i use connected property of tcpclient but it returns the state of last operation. so its not useful.</p>
  
  <p>then i use this code :</p>
  
  <pre><code>bool flag;
  flag = (tcp.Client.Poll(10000, SelectMode.SelectWrite));
  </code></pre>
  
  <p>and </p>
  
  <pre><code> if( tcp.Client.Poll( 0, SelectMode.SelectRead ) )
   {
     byte[] buff = new byte[1];
     if( tcp.Client.Receive( buff, SocketFlags.Peek ) == 0 )
     {
       flag = false;
     }
   }
  </code></pre>
  
  <p>but it does not work properly. </p>
  
  <p>Any idea?</p>
  
  <hr>
  
  <p>this is my code in server side :</p>
  
  <pre><code>   private ArrayList _ClientList = new ArrayList();
  
     public ClsServer(int port)
      {
          _TCPListener = new TcpListener(IPAddress.Any, port);
          _TCPListener.Start();
  
          Thread ListenThread = new Thread(new ThreadStart(ListenForClients));
          ListenThread.IsBackground = true;
          ListenThread.Start();
      }
  
      private void ListenForClients()
      {            
          while (true)
          {
              //blocks until a client has connected to the server
              TcpClient client = this._TCPListener.AcceptTcpClient();
              client.ReceiveTimeout = 0;
  
              //create a thread to handle communication with connected client
              Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
              clientThread.IsBackground = true;
              clientThread.Start(client);
          }
      }
  
      private void HandleClientComm(object client)
      {
          try
          {
              TcpClient tcpClient = (TcpClient)client;               
              AddObject(tcpclient);
  
              int bytesRead;
              string message = "";
              byte[] RecievedPack = new byte[1024 * 1000];
  
              NetworkStream clientStream = tcpClient.GetStream();
              while (true)
              {
                  bytesRead = 0;
                  try
                  {
                      ////blocks until a client sends a message
                      bytesRead = clientStream.Read(RecievedPack, 0, RecievedPack.Length);
                      int Len = BitConverter.ToInt32(RecievedPack, 0);
                      message = UTF8Encoding.UTF8.GetString(RecievedPack, 0, Len);
                  }
                  catch (Exception er)
                  {
                      //When Client is disconnected
                      if (er.GetType() == typeof(IOException))
                      {
                          RemoveObject(client);
                          break;
                      }
                  }                   
                  //message has successfully been received                          
                 // do something
              }
  
              RemoveObject(client);
          }
          catch(Exception e)
          {
             // RemoveObject(client);
          }
      }
  
      private void AddObject(object obj)
      {            
          int totalcount, index;
          totalcount = _ClientList.Count;
          index = 0;
          while (index &lt; totalcount)
          {
              TcpClient alcobj = (TcpClient)_ClientList[index];
              try
              {
                  if (IPAddress.Equals(((IPEndPoint)alcobj.Client.RemoteEndPoint).Address,
                     ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address))
                  {
                      _ClientList.Remove(alcobj);
                      break;
                  }
                  index++;
              }
              catch (Exception er)
              {
                  if (er.GetType() == typeof(ObjectDisposedException))
                      RemoveObject(alcobj);
              }
              finally
              {
                  totalcount = _ClientList.Count;
              }
          }            
           _ClientList.Add(obj);             
      }
  
      private void RemoveObject(object obj)
      {            
          if (_ClientList.IndexOf(obj) &gt; -1)
          {
              _ClientList.Remove(obj);
              SendClientState(IP, false);
          }         
      }
  </code></pre>
  
  <p>and this is the client side :</p>
  
  <pre><code>    public bool IsConnected
      {           
              try
              {
                  if (_TcpClient != null &amp;&amp; _TcpClient.Client != null &amp;&amp; _TcpClient.Client.Connected)
                  {
                      // Detect if client disconnected
                      if (_TcpClient.Client.Poll(0, SelectMode.SelectRead))
                      {
                          byte[] buff = new byte[1];
                          if (_TcpClient.Client.Receive(buff, SocketFlags.Peek) == 0)
                          {
                              // Client disconnected
                              return false;
                          }
                          else
                          {
                              return true;
                          }
                      }
  
                      return true;
                  }
                  else
                  {
                      return false;
                  }
              }
              catch
              {
                  return false;
              }
      }
  
     private void clsClient()
     {
            if(!IsConnected()) 
            {
                    Connecttoserver()
             }
      }
  
      private void ConnectToServer()
      {
          try
          {
              NetworkStream _NetworkStream = _TcpClient.GetStream();
              byte[] _RecievedPack = new byte[1024 * 1000];
              string _Message = string.Empty;
              int _BytesRead;
              int _Length;
  
              while (true)
              {
                  _BytesRead = _NetworkStream.Read(_RecievedPack, 0, _RecievedPack.Length);
                  _Length = BitConverter.ToInt32(_RecievedPack, 0);
                  _Message = UTF8Encoding.UTF8.GetString(_RecievedPack, 4, _Length);
  
                  if (_BytesRead != 0)
                  {
                      if (OnReceive != null)
                          // do something
  
                      _NetworkStream.Flush();
                  }
              }
          }
          catch (Exception exp)
          {
              // do something 
          }
      }
  </code></pre>
  
  <p>in client side, IsConnected() always return false and try to connecttoserver, so the server listener always try to add the client in a list</p>