title: httpclient PostAsJsonAsync errors after (n) messages (port exhaustion?)
body: <p>I have a Console App(webjob) running in Azure that calls an API also running in azure. The Console App posts messages from a csv file.
  When I run the Console App from my local (windows 10) machine (with the api running in azure) it works as expected (no errors).
  When I run the Console App in Azure (with the api running in azure) it processes approx 980 messages and then starts reporting errors</p>
  
  <p>Errors:</p>
  
  <ul>
  <li>"An error occurred while sending the request"</li>
  <li>"Unable to connect to the remote server"</li>
  </ul>
  
  <p>I believe that for some reason when the Console App is running in Azure it is exhausting the ports but I do not know how to resolve this.
  My file has 50,000 rows but as stated only ever reaches approx 980 before issue arrises.
  At the other end the api is posting the received messages to a sql database and to an azure event hub.
  How can I force the application to release the port once the PostAsJsonAsync has completed?
  How can I check to see that the port has been release or how can I check that a port is available?
  If you are aware of any other reasons that i may be getting these errors please help.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public async Task&lt;string&gt; PostMessageToAPI(string aMsg)
  {
      HttpResponseMessage response = null;
      string strResponse = "";    
      try
      {
          using (var client = new HttpClient())
          {
              client.SetBearerToken("access token here");
              client.BaseAddress = new Uri("MyBaseUrl.com");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
              using (response =  await client.PostAsJsonAsync("my/url/goes/here", anEvent.ToLower()))
              {
                  if (response != null)
                  {
                      if (response.StatusCode != HttpStatusCode.OK)
                      {
                          throw new HttpRequestException(response.ReasonPhrase);
                      }
                      else
                      {
                          return await response.Content.ReadAsStringAsync();
                      }
                  }
              }
          }
      }
      catch (HttpRequestException ex)
      {       
          //  exception message states: "An error occurred while sending the request"
      }   
      return strResponse;           
  }
  </code></pre>
  
  <ul>
  <li>Visual Studio 2015 update 3</li>
  <li>.net 4.5.1</li>
  <li>Azure Console App Plan : Basic 1 small</li>
  <li>Azure API App Plan : Basic 1 small</li>
  </ul>