title: How can I use "using" in C# without getting "InvalidOperationException"?
body: <p>I am trying to replce the follwing code that works fine</p>
  
  <pre><code>TcpClient oC = new TcpClient(ip, port);
  oC = new TcpClient(ip, port);
  StreamReader messageReader;
  
  try {
     messageReader = new StreamReader(oC.GetStream(), Encoding.ASCII);
     reply = messageReader.ReadLine();
  }
  </code></pre>
  
  <p>with</p>
  
  <pre><code>try {
     using (messageReader = new StreamReader(oC.GetStream(), Encoding.ASCII))
     {
         reply = messageReader.ReadLine();
     }
  }
  </code></pre>
  
  <p>But I get an <code>InvalidOperationException</code> saying </p>
  
  <blockquote>
    <p>The operation is not allowed on
    non-connected sockets.</p>
  </blockquote>
  
  <p>What is the problem and how can I fix it?</p>
  
  <p><strong>More:</strong> I have oc.Connect before this code, so I am connected and when It wants to be used for the first time is works fine, it is only after that that I get that Exception, I played around a bit with it and now I get:</p>
  
  <blockquote>
    <p>Cannot access a disposed
    object.\r\nObject name:
    'System.Net.Sockets.Socket'.</p>
  </blockquote>