title: WebRequest System.Net.WebException: Unable to connect to the remote server port number is changed in exception
body: <p>I have two self-host .Net application using <strong>System.Web.Http.SelfHost</strong> targeting .Net framework 4.0, both application commnunicate over https.</p>
  
  <p>The first application is using port: 3287 and the second application is using port: 3286 </p>
  
  <p>when I am making a call from the first application to second application using <strong>HttpWebRequest</strong> class I am getting this exception </p>
  
  <pre><code>System.Net.WebException: Unable to connect to the remote server ---&gt; 
  System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:49543
  at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
  at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket&amp; socket, IPAddress&amp; address, ConnectSocketState state, IAsyncResult asyncResult, Exception&amp; exception)
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.GetRequestStream(TransportContext&amp; context)
  at System.Net.HttpWebRequest.GetRequestStream()
  </code></pre>
  
  <p>Here is my c# code </p>
  
  <pre><code>HttpWebRequest request = WebRequest.Create("https://localhost.company.com:3286/"+ EndPoint) as HttpWebRequest;
  ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =&gt; 
  {
      return true;
  };
  request.Method = "POST";
  request.ContentType = "application/json";
  request.ContentLength = data.Length;
  request.AllowAutoRedirect = false;
  request.Expect = "application/json";
  request.GetRequestStream().Write(data, 0, data.Length);
  HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse;
  using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  {
      string s = streamReader.ReadToEnd();
      return s;
  }
  </code></pre>
  
  <p>as i am calling port 3286 but in exception log port number is changed to 49543.</p>
  
  <p>if I call my application through postman I'm able to get the response from it. </p>
  
  <p>certificate localhost.company.com maps to 127.0.0.1 and I have mapped in host file also.</p>