title: C# async SocketException while binding
body: <p>I am working on a client-server application on C# using async sockets. As I listen for connections, I keep getting this error</p>
  <blockquote>
  <p>An unhandled exception of type 'System.Net.Sockets.SocketException'
  occurred in System.dll</p>
  <p>Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted</p>
  </blockquote>
  <p>This is the line where this exception happens:</p>
  <pre><code> _listener.Bind(new IPEndPoint(0, port));
  </code></pre>
  <p>The code is under <code>Listener.Start(int port)</code>. In the main program, here's how I call the method:</p>
  <pre><code>void btnListen_Click(object sender, EventArgs e)
          {
              _listener = new Listener();
              _listener.Accepted += new Listener.SocketAcceptedHandler(listener_Accepted);
              _listener.Start(8192);
          }
  </code></pre>
  <p>I am testing both the client and server applications in my PC.</p>
  <p>Here's the <code>Listener</code> class.</p>
  <pre><code>namespace serverPC
  {
      class Listener
      {
          public delegate void SocketAcceptedHandler(Socket e);
          public event SocketAcceptedHandler Accepted;
  
          Socket _listener;
          public int Port;
  
          public bool Running
          {
              get;
              private set;
          }
  
          public Listener() {Port = 0;}
  
          public void Start(int port)
          {
              if (Running)
                  return;
  
              _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              _listener.Bind(new IPEndPoint(0, port)); // This is where the SocketException error occurs
              _listener.Listen(0);
  
              _listener.BeginAccept(acceptedCallback, null);
              Running = true;
          }
  
          public void Stop()
          {
              if (!Running)
                  return;
  
              _listener.Close();
              Running = false;
          }
  
          void acceptedCallback(IAsyncResult ar)
          {
              try
              {
                  Socket s = _listener.EndAccept(ar);
  
                  if (Accepted != null)
                  {
                      Accepted(s);
                  }
              }
              catch
              {
  
              }
  
              if (Running)
              {
                  try
                  {
                      _listener.BeginAccept(acceptedCallback, null);
                  }
                  catch
                  {
  
                  }
              }
          }
      }
      
  }
  </code></pre>
  <p>Here's the <code>Client</code> class.</p>
  <pre><code>namespace serverPC
  {
      struct ReceiveBuffer
      {
          public const int BUFFER_SIZE = 1024;
          public byte[] Buffer;
          public int ToReceive;
          public MemoryStream BufStream;
    
          public ReceiveBuffer(int toRec)
          {
              Buffer = new byte[BUFFER_SIZE];
              ToReceive = toRec;
              BufStream = new MemoryStream(toRec);
          }
  
          public void Dispose()
          {
              Buffer = null;
              ToReceive = 0;
              Close();
              if (BufStream != null)
                  BufStream.Dispose();
          }
  
          public void Close()
          {
              if (BufStream != null &amp;&amp; BufStream.CanWrite)
              {
                  BufStream.Close();
              }
          }
      }
  
      class Client
      {
          byte[] lenBuffer;
          ReceiveBuffer buffer;
          Socket _socket;
  
          public IPEndPoint EndPoint
          {
              get
              {
                  if (_socket != null &amp;&amp; _socket.Connected)
                  {
                      return (IPEndPoint)_socket.RemoteEndPoint;
                  }
  
                  return new IPEndPoint(IPAddress.None, 0);
              }
          }
  
          public delegate void DisconnectedEventHandler(Client sender);
          public event DisconnectedEventHandler Disconnected;
          public delegate void DataReceivedEventHandler(Client sender, ReceiveBuffer e);
          public event DataReceivedEventHandler DataReceived;
  
          public Client(Socket s)
          {
              _socket = s;
              lenBuffer = new byte[4];
          }
  
          public void Close()
          {
              if (_socket != null)
              {
                  _socket.Disconnect(false);
                  _socket.Close();
              }
  
              buffer.Dispose();
              _socket = null;
              lenBuffer = null;
              Disconnected = null;
              DataReceived = null;
          }
  
          public void ReceiveAsync()
          {
              _socket.BeginReceive(lenBuffer, 0, lenBuffer.Length, SocketFlags.None, receiveCallback, null);
          }
  
          void receiveCallback(IAsyncResult ar)
          { 
              try
              {
                  int rec = _socket.EndReceive(ar);
  
                  if (rec == 0)
                  {
                      if (Disconnected != null)
                      {
                          Disconnected(this);
                          return;
                      }
  
                      if (rec != 4)
                      {
                          throw new Exception();
                      }
                  }
              }
  
              catch (SocketException se)
              {
                  switch (se.SocketErrorCode)
                  {
                      case SocketError.ConnectionAborted:
                      case SocketError.ConnectionReset:
                          if (Disconnected != null)
                          {
                              Disconnected(this);
                              return;
                          }
                          break;
                  }
              }
              catch (ObjectDisposedException)
              {
                  return;
              }
              catch (NullReferenceException)
              {
                  return;
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
                  return;
              }
  
              buffer = new ReceiveBuffer(BitConverter.ToInt32(lenBuffer, 0));
  
              _socket.BeginReceive(buffer.Buffer, 0, buffer.Buffer.Length, SocketFlags.None, receivePacketCallback, null);
  
          }
  
          void receivePacketCallback(IAsyncResult ar)
          {
              int rec = _socket.EndReceive(ar);
  
              if (rec &lt;= 0)
              {
                  return;
              }
  
              buffer.BufStream.Write(buffer.Buffer, 0, rec);
  
              buffer.ToReceive -= rec;
  
              if(buffer.ToReceive &gt; 0)
              {
                  Array.Clear(buffer.Buffer, 0, buffer.Buffer.Length);
  
                  _socket.BeginReceive(buffer.Buffer, 0, buffer.Buffer.Length, SocketFlags.None, receivePacketCallback, null);
                  return;
              }
  
              if (DataReceived != null)
              {
                  buffer.BufStream.Position = 0;
                  DataReceived(this, buffer);
              }
  
              buffer.Dispose();
  
              ReceiveAsync();
          }
      }
  }
  </code></pre>
  <p>Why do I get the SocketException error? Any help is appreciated. Thanks!</p>