title: How to release ports and stop listening after sending a message with the UdpClient?
body: <p>I have two UDP clients: </p>
  
  <pre><code>UdpClient _udpReceiver
  </code></pre>
  
  <p>and</p>
  
  <pre><code>UdpClient _udpSender
  </code></pre>
  
  <p>For receiving a message I'm using <code>_udpReceiver.BeginReceive(...);</code></p>
  
  <p>And for sending: <code>_udpSender.BeginSend(data, data.Length, new AsyncCallback(StopCallback, _udpSender);</code></p>
  
  <p>What I'm trying to achieve is to stop any sending and receiving and somehow unbind the ports which _udpReceiver and _udpSender are using. But with no success:</p>
  
  <pre><code>  private void StopCallback(IAsyncResult ar)
      {
         try
          {
              UdpClient client = (UdpClient)ar.AsyncState;
              client.EndSend(ar);     
              client.Close(); //app crashes here   
              _udpSender.Client.Close(); //and here              
              _udpReceiver.Client.Close(); //and here too
          }
          catch (ObjectDisposedException ex)
          {
              //and this one is never getting caught
          }
      }
  </code></pre>
  
  <hr>
  
  <p><strong>UPD: attached link to a project which reproduces the crash</strong></p>
  
  <p><a href="http://1drv.ms/1kWWJKA" rel="nofollow">http://1drv.ms/1kWWJKA</a></p>