title: How to handle reading and writing to the same socket
body: <p>I establish a connection to my socket server and then start reading and writing data enormously. 
  An exception occurs immediately:</p>
  
  <p><strong>An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code</strong></p>
  
  <p><strong>Additional information: The stream is currently in use by a previous operation on the stream.</strong></p>
  
  <p>How can I detect that </p>
  
  <pre><code>_reader.ReadLineAsync();
  </code></pre>
  
  <p>is currently receiving some data and I should wait with the writing and vice versa?</p>
  
  <p>This class is used by a client that basically sends the received data from server back to the server.</p>
  
  <pre><code>public class TcpClientWorker2 
  {
      private TcpClient _client;
      private Thread _t;
      private NetworkStream _networkStream;
      private StreamWriter _writer;
      private StreamReader _reader;
      public TcpClientWorker2()
      {
          var ipHostInfo = Dns.Resolve(Dns.GetHostName());
          var ipAddress = ipHostInfo.AddressList[0];
  
          _client = new TcpClient();
          _client.Connect(ipAddress, 10000);
          _t = new Thread(Run);
      }
  
      public async void Run()
      {
          _networkStream = _client.GetStream();
          _reader = new StreamReader(_networkStream);
          _writer = new StreamWriter(_networkStream) { AutoFlush = true };
          while (true)
          {
              var data = await _reader.ReadLineAsync();
              //perform callback (send the data immediately back to server)
          }
      }
  
      public void Start()
      {
          _t.Start();
      }
      public async Task Send(string text)
      {
          await _writer.WriteLineAsync(text);
      }
  }
  </code></pre>