title: HttpWebRequest getRequestStream hangs on multiple runs
body: <p>I've written some code to send and read text from a listener. This runs fine on the 1st and 2nd exchange, but on the 3rd send there's a long delay between calling <code>GetRequestStream()</code> and the actual writing of the data.</p>
  <p>I've disposed the outstream on the send side, as well as the stream reader, and the input stream on the read side as recommended here: <a href="https://stackoverflow.com/questions/2242866/httpwebrequest-timeout-hangs-help">Does anyone know why I receive an HttpWebRequest Timeout?</a></p>
  <p>And it still hangs on the 3rd attempt to send info. It definitely seems to be hanging at <code>GetRequestStrean()</code> in <code>SendMessage()</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>public void SendMessage(string message)
  {
      HttpWebRequest request;
      string sendUrl;
      
      sendUrl = &quot;http://&quot; + termIPAddress + &quot;:&quot; + sendPort + &quot;/&quot;;
      
      Uri uri = new Uri(sendUrl);
      
      Console.WriteLine(&quot;http://&quot; + termIPAddress + &quot;:&quot; + sendPort + &quot;/&quot;);
      
      ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
      
      servicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
      servicePoint.ConnectionLeaseTimeout = 300;
      
      
      request = (HttpWebRequest)WebRequest.Create(sendUrl);
      request.KeepAlive = false;
      request.Method = &quot;POST&quot;;
      request.ProtocolVersion = HttpVersion.Version11;
      request.ContentType = &quot;application/x-www-form-urlencoded&quot;;
      request.Headers.Add(&quot;SourceIP&quot;, localIPAddress);
      request.Headers.Add(&quot;MachineName&quot;, localName);
      requestStarted = true;
      
      
      byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
      request.ContentLength = buffer.Length;
          
      try
      {
          using (Stream output = request.GetRequestStream())
          {
              output.Write(buffer, 0, buffer.Length);
              output.Close();
              request = null;
          }
      }
      catch(WebException wE)
      {
          Console.WriteLine(wE.Message);
      }
  }
  </code></pre>
  <p>And this is the read portion :</p>
  <pre class="lang-cs prettyprint-override"><code>public string GetMessage()
  {
      Console.WriteLine(&quot;Entering actual listener&quot;);
      string s;
      string sourceIP;
      
      NameValueCollection headerList;
      
      HttpListenerContext context = terminalListener.GetContext();
      HttpListenerRequest request = context.Request;
          
      headerList = request.Headers;
      sourceIP = headerList.GetValues(&quot;SourceIP&quot;)[0];
      termName = headerList.GetValues(&quot;MachineName&quot;)[0];
      termIPAddress = sourceIP;
      using (System.IO.Stream body = request.InputStream)
      {
          System.Text.Encoding encoding = request.ContentEncoding;
          using (System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding))
          {
              s = reader.ReadToEnd();
              body.Close();
              reader.Close();
          }
      }
      
      return termName + &quot; : &quot; + s;    
  }
  </code></pre>
  <p>I also tried to add an IP End Point bind but have to be honest, I don't fully understand this piece of the code:</p>
  <pre class="lang-cs prettyprint-override"><code>private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
  {
      int portNumber = Convert.ToInt32(sendPort);
      IPEndPoint IEP = new IPEndPoint(IPAddress.Parse(localIPAddress), 0); 
      
      Console.WriteLine(Convert.ToString(IEP));
      return IEP;  
  }
  </code></pre>