title: TcpClient: How do I close and reconnect it again?
body: <p>Hello and thanks for your help.
  This time I would like to ask about TcpClient. 
  I have a server program and I am writing a client program.
  This client uses TcpClient. It starts by creating a new client</p>
  
  <pre><code>clientSocket=new TcpClient(); 
  </code></pre>
  
  <p>(By the way, can this cause exceptions? just in case I put it inside a try-catch but I am not sure if that is really necessary)
  Anyway, later I enter a loop and inside this loop I connect to the server</p>
  
  <pre><code>clientSocket.Connect("xx.xx.xx.xx",port);
  </code></pre>
  
  <p>Then I create a NetworkStream with</p>
  
  <pre><code>clientStream=clientSocket.GetStream();
  </code></pre>
  
  <p>and then start waiting for data from the server through Read. I know this is blocking so I also set a ReadTimeOut (say 1 second)</p>
  
  <p>Anyway, so far so good. 
  Later if I don't receive anything from the server, I attempt to send something to it. If this keeps happening for say 3 times I <strong>want to close the connection and reconnect to the server again</strong></p>
  
  <p>(notice that a whole different problem is when the server somehow is down, cause that causes other kinds of errors in the client-perhaps I will ask about that later)</p>
  
  <p>So, what do I do?</p>
  
  <pre><code> if(clientSocket.Connected)
              {
                  Console.WriteLine("Closing the socket");
                  clientSocket.Close();
              }
  </code></pre>
  
  <p>I close the socket. 
  The loop is finished so I go again to the beginning and try to connect to the server.</p>
  
  <pre><code>clientSocket.Connect("xx.xx.xx.xx",port);
  </code></pre>
  
  <p>However this causes an error(an unhandled exception actually) "Can not access a disposed object" </p>
  
  <p>So <strong>my question is</strong>  <em>How can I close and reconnect to the server again??</em></p>
  
  <p>Thanks again for any help</p>