title: TCP connections are stacked until 65k when using HttpWebRequest fetching function - internet connectivity is lost
body: <p>I don't understand what may cause this error</p>
  
  <p>I am using the below function with about 1000 concurrent connections</p>
  
  <p>Each connection uses a different webproxy</p>
  
  <p>After a while like 15 minutes working, the established TCP connection count starts to stack and internet connectivity becomes lost</p>
  
  <p>When i do not use any webproxy, i do not encounter any error</p>
  
  <p>I am using below function to retrieve active TCP connections count</p>
  
  <pre><code>var properties = IPGlobalProperties.GetIPGlobalProperties();
  </code></pre>
  
  <p>I don't see any leak in my function</p>
  
  <p>So i need your help to solve this annoying problem</p>
  
  <p>c# .net 4.6.2</p>
  
  <p>Here the statuses of active TCP Connections when this problem occurs</p>
  
  <p><a href="https://i.stack.imgur.com/9vrpN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9vrpN.png" alt="enter image description here"></a></p>
  
  <pre><code>public static cs_HttpFetchResults func_fetch_Page(
      string srUrl, int irTimeOut = 60,
      string srRequestUserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0",
      string srProxy = null, int irCustomEncoding = 0, bool blAutoDecode = true, bool blKeepAlive = true)
  {
      cs_HttpFetchResults mycs_HttpFetchResults = new cs_HttpFetchResults();
      mycs_HttpFetchResults.srFetchingFinalURL = srUrl;
  
      HttpWebRequest request = null;
      WebResponse response = null;
  
      try
      {
          request = (HttpWebRequest)WebRequest.Create(srUrl);
          request.CookieContainer = new System.Net.CookieContainer();
  
          if (srProxy != null)
          {
              string srProxyHost = srProxy.Split(':')[0];
              int irProxyPort = Int32.Parse(srProxy.Split(':')[1]);
              WebProxy my_awesomeproxy = new WebProxy(srProxyHost, irProxyPort);
              my_awesomeproxy.Credentials = new NetworkCredential();
              request.Proxy = my_awesomeproxy;
          }
          else
          {
              request.Proxy = null;
          }
  
          request.ContinueTimeout = irTimeOut * 1000;
          request.ReadWriteTimeout = irTimeOut * 1000;
          request.Timeout = irTimeOut * 1000;
          request.UserAgent = srRequestUserAgent;
          request.KeepAlive = blKeepAlive;
          request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  
          WebHeaderCollection myWebHeaderCollection = request.Headers;
          myWebHeaderCollection.Add("Accept-Language", "en-gb,en;q=0.5");
          myWebHeaderCollection.Add("Accept-Encoding", "gzip, deflate");
  
          request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
  
          using (response = request.GetResponse())
          {
              using (Stream strumien = response.GetResponseStream())
              {
                  Encoding myEncoding = Encoding.UTF8;
                  string srContentType = "";
  
                  if (response.ContentType != null)
                  {
                      srContentType = response.ContentType;
                      if (srContentType.Contains(";"))
                      {
                          srContentType = srContentType.Split(';')[1];
                      }
                      srContentType = srContentType.Replace("charset=", "");
                      srContentType = func_Process_Html_Input(srContentType);
                  }
  
                  try
                  {
                      myEncoding = Encoding.GetEncoding(srContentType);
                  }
                  catch
                  {
                      myEncoding = irCustomEncoding == 0 ? Encoding.UTF8 : Encoding.GetEncoding(irCustomEncoding);
                  }
  
                  using (StreamReader sr = new StreamReader(strumien, myEncoding))
                  {
                      mycs_HttpFetchResults.srFetchBody = sr.ReadToEnd();
                      if (blAutoDecode == true)
                      {
                          mycs_HttpFetchResults.srFetchBody = HttpUtility.HtmlDecode(mycs_HttpFetchResults.srFetchBody);
                      }
                      mycs_HttpFetchResults.srFetchingFinalURL = Return_Absolute_Url(response.ResponseUri.AbsoluteUri.ToString(), response.ResponseUri.AbsoluteUri.ToString());
                      mycs_HttpFetchResults.blResultSuccess = true;
                  }
              }
          }
  
          if (request != null)
              request.Abort();
          request = null;
      }
      catch (Exception E)
      {
          if (E.Message.ToString().Contains("(404)"))
              mycs_HttpFetchResults.bl404 = true;
  
          csLogger.logCrawlingErrors("crawling failed url: " + srUrl, E);
  
      }
      finally
      {
          if (request != null)
              request.Abort();
          request = null;
  
          if (response != null)
              response.Close();
          response = null;
      }
  
      return mycs_HttpFetchResults;
  }
  </code></pre>