title: WSACancelBlockingCall exception
body: <p>Ok, I have a strange exception thrown from my code that's been bothering me for ages.</p>
  
  <pre><code>System.Net.Sockets.SocketException: A blocking operation was interrupted by a call to WSACancelBlockingCall
     at System.Net.Sockets.Socket.Accept()
     at System.Net.Sockets.TcpListener.AcceptTcpClient()
  </code></pre>
  
  <p>MSDN isn't terribly helpful on this : <a href="http://msdn.microsoft.com/en-us/library/ms741547(VS.85).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms741547(VS.85).aspx</a> and I don't even know how to begin troubleshooting this one. It's only thrown 4 or 5 times a day, and never in our test environment. Only in production sites, and on ALL production sites. </p>
  
  <p>I've found plenty of posts asking about this exception, but no actual definitive answers on what is causing it, and how to handle or prevent it.</p>
  
  <p>The code runs in a separate background thread, the method starts :</p>
  
  <pre><code>public virtual void Startup()
      {
       TcpListener serverSocket= new TcpListener(new IPEndPoint(bindAddress, port));    
          serverSocket.Start();
  </code></pre>
  
  <p>then I run a loop putting all new connections as jobs in a separate thread pool. It gets more complicated because of the app architecture, but basically:</p>
  
  <pre><code>   while (( socket = serverSocket.AcceptTcpClient()) !=null) //Funny exception here
      {
           connectionHandler = new ConnectionHandler(socket, mappingStrategy);
           pool.AddJob(connectionHandler);
      }
    }
  </code></pre>
  
  <p>From there, the <code>pool</code> has it's own threads that take care of each job in it's own thread, separately.</p>
  
  <p>My understanding is that AcceptTcpClient() is a blocking call, and that somehow winsock is telling the thread to stop blocking and continue execution.. but why? And what am I supposed to do? Just catch the exception and ignore it? </p>
  
  <hr>
  
  <p>Well, I do think some other thread is closing the socket, but it's certainly not from my code. 
  What I would like to know is: is this socket closed by the connecting client (on the other side of the socket) or is it closed by my server. Because as it is at this moment, whenever this exception occurs, it shutsdown my listening port, effectively closing my service. If this is done from a remote location, then it's a major problem. </p>
  
  <p>Alternatively, could this be simply the IIS server shutting down my application, and thus cancelling all my background threads and blocking methods?</p>