title: How to check if connection is open
body: <p>I have a problem when i'm trying to avoid using a <code>Thread.sleep(400)</code>
  my code is like this:</p>
  
  <pre><code>System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
  clientSocket = Connect(IP, Port);
  Thread.Sleep(400);
  
  NetworkStream networkStream = clientSocket.GetStream();
  Send(networkStream, "My Data To send");
  networkStream.Flush();
  </code></pre>
  
  <p>and my send() method:</p>
  
  <pre><code>public static void Send(NetworkStream networkStream, string Data)
  {
      int range = 1000;
      int datalength = 0;
      foreach (string data in Enumerable.Range(0, Data.Length / range).Select(i =&gt; Data.Substring(i * range, range)))
      {
          byte[] outStream = System.Text.Encoding.ASCII.GetBytes(data);
          networkStream.Write(outStream, 0, outStream.Length);
          datalength = datalength + range;
          Thread.Sleep(50);
      }
      byte[] LastoutStream = System.Text.Encoding.ASCII.GetBytes(Data.Substring(datalength, Data.Length - datalength) + "$EOS$\r\n");
      networkStream.Write(LastoutStream, 0, LastoutStream.Length);
  }
  </code></pre>
  
  <p>the Connect method:</p>
  
  <pre><code> protected static System.Net.Sockets.TcpClient Connect(string Ip, int Onport)
      {
          //start connection
          System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
          try
          {
              clientSocket.Connect(Ip, Onport);
          }
          catch
          {
              clientSocket.Connect("LocalHost", Onport);
          }
          return clientSocket;
      }
  </code></pre>
  
  <p>is there a way to check if the stream is ready to be used?</p>