title: Issue WebRequest while reading NetworkStream of TcpListener
body: <p>Following on from the post <a href="https://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c">How to create a simple proxy in C#?</a> I have been playing around with implementing a basic proxy.</p>
  
  <p>Where I am getting stuck and confused is trying to issue a <code>WebRequest</code> with the information provided in the original request. </p>
  
  <p>Using the following code.</p>
  
  <pre><code>WebRequest webRequest = WebRequest.Create("http://www.google.com");
  
  (webRequest as HttpWebRequest).UserAgent = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";
  webRequest.Method = "GET";
  
  WebResponse webResponse = webRequest.GetResponse();
  Stream responseStream = webResponse.GetResponseStream();
  byte[] responseBytes = responseStream.ReadFully();
  </code></pre>
  
  <p>I can successfully issue a request and return the page content.</p>
  
  <p>However when I put it inside a Proxy request (IE: <code>TcpListener</code>) like such.</p>
  
  <pre><code>TcpListener _listener = new TcpListener(IPAddress.Any, 1234);
  this._listener.Start();
  byte[] bytes = new byte[1024];
  while (true)
  {
      TcpClient client = this._listener.AcceptTcpClient();
      NetworkStream networkStream = client.GetStream();
      int i = networkStream.Read(bytes, 0, bytes.Length);
      while (i != 0)
      {
          string data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
          RequestHeader header = new RequestHeader(data.ToUpper());
          WebRequest webRequest = WebRequest.Create(header.URL);
          (webRequest as HttpWebRequest).UserAgent = header.UserAgent;
          webRequest.Method = "GET";
          WebResponse webResponse = webRequest.GetResponse(); //It gets here and never returns
          Stream responseStream = webResponse.GetResponseStream();
          byte[] responseBytes = responseStream.ReadFully();
          networkStream.Write(responseBytes, 0, responseBytes.Length);
          i = networkStream.Read(bytes, 0, bytes.Length);
      }
      client.Close();
  }
  </code></pre>
  
  <p>It blocks at the line <code>WebResponse webResponse = webRequest.GetResponse();</code> and never returns.</p>
  
  <p>This has definitely got nothing to do with the data provided by the <code>RequestHeader</code> class I created as I've also tried hardcoding the values.</p>
  
  <p>I'm assuming I'm missing something fundamental about the way sockets work in such a scenario and the approach required. Hopefully someone can clarify for me.</p>