title: C# Socket.Receive() SocketException: A request to send or receive data was disallowed because the socket is not connected
body: <p>I'm struggling with Windows Socket programming in C#, .Net 4.5 on Windows 8.1.</p>
  <p>So, I'm doing a pretty simple operation. I'm implementing a client-server type system all to be run on a the local machine, communicating over sockets. The server is a Windows Service, the client is a user application.</p>
  <p>The service, upon request from the client, performs some OCR computation then returns the results to the requesting process. The service has a listening socket that accepts incoming requests at all times, a new socket is created and used to respond to each requests. The port # for the new responding socket is specified in the request. This is done to allow the listening socket to handle concurrent requests.</p>
  <p>The service's listening socket is working fine. I am able to receive requests from a user application connected to the listening socket. The problem occurs when the roles are reversed: the service needs to connect to the Socket owned by the client and send the OCR results over the client-owned socket. Let's get into the code:</p>
  <p><strong>Server Side</strong></p>
  <pre><code>this.LocalAddress = Dns.GetHostEntry(&quot;localhost&quot;).AddressList[0]; // localhost ip addr
  
  ...
  //*************** Receive request
  //*************** Perform computation
  ...
  
  // respond to requester with results from OCR 
  using (Socket responder = new Socket(this.LocalAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  {
      IPEndPoint requestEndpoint = new IPEndPoint(this.LocalAddress, request.portNum);
      responder.Connect(requestEndpoint); //same endpoint as client
  
      if(failure)
          responder.Send(Encoding.UTF8.GetBytes(this.OCRFailureMessage));
      else
          responder.Send(Encoding.UTF8.GetBytes(ocrResults.ToString()));
  }
  </code></pre>
  <p>No exceptions are thrown, service continues as if everything happened successfully.</p>
  <p><strong>Client Side</strong></p>
  <pre><code>//localhost address
  IPAddress address = Dns.GetHostEntry(&quot;localhost&quot;).AddressList[0]; // localhost ip addr
  
  // endpoint to listen on for response from StartOCRService
  IPEndPoint myEndpoint = new IPEndPoint(address, 50002);
  
  // create socket to receive response on and bind to listening endpoint
  Socket receiver = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  receiver.Bind(myEndpoint);
  receiver.Listen(10);
  
  ...
  //************ Connect to service listening socket
  //************ Send OCR request to service (myEndpoint port# included)
  ...
  
  // accept connection from service
  receiver.Accept();
  
  // receive data from service
  byte[] returnData = new byte[1024];
  int numBytesReceived = receiver.Receive(returnData); //EXCEPTION THROWN
  </code></pre>
  <p>The exception is thrown in the client side on the <code>receiver.Receive()</code> call. Here are the exception details:</p>
  <blockquote>
  <p>Type: SocketException</p>
  <p>Message: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.</p>
  <p>ErrorCode: 10057 (Socket is not connected)</p>
  </blockquote>
  <p>So, where I'm hung up is how the client's socket is not connected. The line <code>receiver.Accept()</code> is a blocking call that waits for the server to call <code>responder.Connect()</code>. Stepping through both sides concurrently I see the client wait on <code>receiver.Accept()</code> until the server calls <code>responder.Connect()</code>. I take this to mean that the client socket has accepted a connection. Can anybody spot the reason why the client's socket is remaining unconnected and this exception is being thrown?</p>
  <p>All help is greatly appreciated, thanks SO!</p>
  <p><strong>EDIT: 7/29/2015 5:07p</strong></p>
  <p>Some additional details that may be helpful. After the service calls <code>responder.Connect()</code> responder.Connected is <em>true</em> and responder.RemoteEndpoint is the endpoint that the client is listening on (address and port# confirmed).</p>
  <p>The same cannot be said for the client however. After the <code>receiver.Accept()</code> call passes receiver.Connected is <em>false</em> and receiver.RemoteEndpoint threw the same exception as above.</p>
  <p>Digging further into receiver's members I found receiver.DontFragment threw a System.NotSupportedException with the message</p>
  <blockquote>
  <p>This protocol version is not supported.</p>
  </blockquote>
  <p>Something wrong with the protocol I'm using? I'm using TCP across the board. Perhaps it has something to do with the address families?</p>