title: Is there a way to for UDP .net socket to release a bound port when the socket is closed?
body: <p>For this question, I'm running windows 7 64 bit, .net framework 3.5 </p>
  
  <p>What I really want to know is </p>
  
  <ol>
  <li>Is there a way to unbind/release a port that has been bound?</li>
  <li>When I set the socketoption reuse address I am able to bind to the port but I never receive data using that address/port?</li>
  </ol>
  
  <p>I'm looking at what ports are bound using netstat -apd udp </p>
  
  <p>The test code I'm using is below, normally I would not close the port after receiving one packet.  </p>
  
  <pre><code>EndPoint endpoint = new IPEndPoint(state.IPAddress, state.Port);
  Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);
  socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);                    
  socket.Bind(endpoint);
  
  
  ReadStateObject stateObject = new ReadStateObject();
  stateObject.socket = socket;
  stateObject.Port = state.Port;
  stateObject.IPAddress = state.IPAddress;
  stateObject.BurstDataReceivedEvent = state.BurstDataReceivedEvent;
  stateObject.shutdownRequested = state.StopRequestedEvent;
  socket.BeginReceiveFrom(stateObject.buffer,
                          0,
                          stateObject.buffer.Length,
                          SocketFlags.None, // Was 0 which is likely the same enumeration but this is clearer
                          ref endpoint,
                          new AsyncCallback(BurstUdpListener.SocketBeginReceiveAsyncCallback),
                           stateObject);
  
  // wait for read to complete... or the thread to be asked to stop
  while (stateObject.readFinished.WaitOne(100, false) == false)
  {
      // has this thread been requested to stop? if so, cancel the pending read
      if (state.StopRequestedEvent.WaitOne(1, false) == true)
      {
          stop = true;
          break;
       }
   }
  
   socket.Shutdown(SocketShutdown.Both);
   socket.Close();
   socket = null;
  
  
  
      private static void SocketBeginReceiveAsyncCallback(IAsyncResult ar)
      {
  
              if (ar.IsCompleted)
              {
                  ReadStateObject state = ar.AsyncState as ReadStateObject;
                  if (state != null)
                  {
                      if (state.shutdownRequested.WaitOne(1, false))
                          return;
                      EndPoint endpoint = new IPEndPoint(state.IPAddress, state.Port);
                      int bytesReceived = state.socket.EndReceiveFrom(ar, ref endpoint); // for some reason throws error here on changes to settings
                      if (state.BurstDataReceivedEvent != null)
                      {
                          state.BurstDataReceivedEvent(null, new BurstDataReceivedEventArgs(state.buffer, bytesReceived));
                      }
                      state.readFinished.Set();
                  }
  
           }            
  
   }
  </code></pre>