title: TcpClient Auto-Reconnect
body: <p>What is the best way to make a Tcpclient auto reconnect to the server when
  it's disconnected (e.g. by the server itself)?</p>
  
  <p>The code I'm currently using is:</p>
  
  <pre><code>public void ClientWork()
  {
          TcpClient client = new TcpClient();
  
          try
          {
              try
              {
                  client.Connect(ip, port);
              }
              catch(Exception ex)
              {
                  logger.ErrorFormat("client.Connect: {0}", ex.Message);
                  return false;
              }
  
              NetworkStream ns = client.GetStream();
              byte[] buff;
  
              while (__bRunning)
              {
                  buff = new byte[1000];
                  ns.Read(buff, 0, 1000);
  
                  string line = System.Text.Encoding.Default.GetString(buff);
              }
  
              //ns.Close();
              client.Close();
          }
          catch(Exception e)
          {
              //Reconnect?
              client.Close();
              client = null;
  
              return false;
          }
  }
  </code></pre>
  
  <p>I'm using C# .NET</p>