title: System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine
body: <p>I'd made a windows service remote server program that will able to communicate to a Windows CE device(as remote client) listening for a broadcast request, save a request to file, examine it and send back the requested file content being trace. I've implemented it in (c#) asynchronous transfer mode. It is working properly, but just 2 days ago I've encountered this socket exception:  </p>
  
  <p><code>System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)</code></p>
  
  <p>this is my code it points to the exception </p>
  
  <pre><code>     public void AcceptCallback(IAsyncResult result)
       { 
  
            ConnectionInfo connection = new ConnectionInfo();
            try
            {
                long connection_number;
  
                //finish accept
                Socket s;
                s = (Socket)result.AsyncState;
                connection.socket = s.EndAccept(result);
  
                connection.socket.Blocking = false;
  
                connection.Buffer = new byte[10000]; //1kb buffer minimum
  
  
       orionIP = ((IPEndPoint)connection.socket.RemoteEndPoint).Address.ToString();
                 lock (connections) connections.Add(connection);
               IPHostEntry hostName = Dns.GetHostEntry(orionIP);
                 machineName = hostName.HostName;
                 lock (connections) connections.Add(connection);
                  //count client connected
                connection_number = Interlocked.Increment(ref connection_count);
  
                //start receive -&gt; error here 
     connection.socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length,
                    SocketFlags.None, new AsyncCallback(ReceiveCallBack), connection);
  
              //start new accept 
               serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
  
            }//end try 
            catch (SocketException ex)
            { //later log files for error events  - &gt; ex.SocketErrorCode
                string path = "eventlogs.txt";
                //create error log files to write error here 
                File.WriteAllText(path, ex.ToString());
                CloseConnection(connection);
            }
            catch (Exception exp)
            {
                string path = "eventlogs.txt";
                //create error log files to write error here 
                File.WriteAllText(path, exp.ToString());
                CloseConnection(connection);
                // log error file later ("Socket Exception:" + exp.ToString());
            }
       }
  </code></pre>
  
  <p>I have no major revision made to it when the exception occurred. I followed all the instruction along with others encountering the same problem such as turn off the firewall, <code>maxusePort</code>, check network connections and devices if properly configured, buffer size. I also check the race condition and <code>WSAECONNABORTED</code> issues i might dealing with.  </p>
  
  <p>Can anyone relates to this problem give me some additional solutions? Any additional knowledge will be a great help . </p>
  
  <p><strong>Receive callback</strong></p>
  
  <pre><code>     private void ReceiveCallBack(IAsyncResult result)
       {
           //06.10 added thread for writing in the file 
  
           //added part review () 
           //getemp file 
           string pathwrite = CreateTmpFile();//"streamdata.txt";
      ConnectionInfo connection = (ConnectionInfo)result.AsyncState;
           try
              { 
  
               int bytesRead = connection.socket.EndReceive(result);
  
               if (bytesRead &gt; 0)
               {
                   lock (serverlock)
                   {
                       connection.sb.Append(Encoding.UTF8.GetString(connection.Buffer, 0, bytesRead));
                   }
  
                   lock (connections)
                   {
                       foreach (ConnectionInfo conn in connections)
                       {
                           if (connection != conn)
                           {
               conn.socket.Send(connection.Buffer, bytesRead, SocketFlags.None);
  
                           }
                       }
                   }
     connection.socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None,new AsyncCallback(ReceiveCallBack), connection);        
               }
               else
               {
                   string strContent = connection.sb.ToString();
                   File.WriteAllText(pathwrite, strContent);
                  AccessData();
                   CloseConnection(connection);
               }
  
           }//end try 
  
           catch(SocketException ex){
               string path = "eventlogs2.txt";
               //create error log files to write error here 
               File.WriteAllText(path, ex.ToString());
          // CloseConnection(connection);
           }
           catch(Exception ex)
           {
               string path = "eventlogs2.txt";
               //create error log files to write error here 
               File.WriteAllText(path, ex.ToString());
            //   CloseConnection(connection);
           }
  
  
       }
  </code></pre>