title: Socket exception in Metro-style app when sharing is turned on for a network
body: <p>I'm working on a Metro-style app that communicates with our server via a web service.  Early on, I discovered that I needed to add the "Home or Work Networking" (privateNetworkClientServer) and "Internet Client" (internetClient) capabilities in the manifest, or else the app would hit exceptions when attempting to make web service calls to a remote host.</p>
  
  <p>Now, however, on some machines I'm getting the following socket exception when the app tries to call our web service:</p>
  
  <pre><code>System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at https://[my_service_url] that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---&gt; System.Net.WebException: Unable to connect to the remote server
  ---&gt; 
  System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions [my_server_ip_address]:443
     at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
     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.EndGetRequestStream(IAsyncResult asyncResult, TransportContext&amp; context)
     at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStreamAsyncResult.CompleteGetRequestStream(IAsyncResult result)
     --- End of inner exception stack trace ---
     at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
     at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
     at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
     at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.&lt;&gt;c__DisplayClass5`1.&lt;CreateGenericTask&gt;b__4(IAsyncResult asyncResult)
     at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  </code></pre>
  
  <p>Interestingly enough, if I disable sharing (right-click the wifi connection, select "Turn sharing on or off", and choose "No, don't turn on sharing or connect to devices"), the problem goes away and web service calls succeed.  If then go back and turn sharing back on (select "yes" instead of "no"), the problem returns.</p>
  
  <p>So it appears that the problem is related to whether the network has sharing and devices enabled.  I'm not completely sure of the mapping, but I think this basically boils down to the distinction between "home/work" and "public" networks; in my case the web service calls fail when the network is "home/work" and succeed when the network is "public" - but my app has both types of networking capabilities declared, and this issue doesn't occur on all boxes.  There is also no firewall or other networking restriction in place; it's a bare-bones Win8 install with no additional tweaking.</p>
  
  <p>Finally, this isn't isolated to web services - the same app has iframes that fail or succeed under the exact some conditions as the web services.</p>