title: (Apparently) Gracefully Closed UDPClient leaves the socket blocked
body: <p>The following code, despite apparently closing the UDP Socket, leaves it hanging and unable to reconnect to the same address / port.</p>
  
  <p>These are the class variables I use:</p>
  
  <pre><code>    Thread t_listener;
      List&lt;string&gt; XSensAvailablePorts;
      private volatile bool stopT_listener = false;        
      volatile UdpClient listener;
      IPEndPoint groupEP;
  </code></pre>
  
  <p>I create and launch a new thread with a method which will handle the Socket connection and listening:</p>
  
  <pre><code>private void startSocketButton_Click(object sender, RoutedEventArgs e)
          {
              stopT_listener = false;
              closeSocketButton.IsEnabled = true;
              startSocketButton.IsEnabled = false;
              t_listener = new Thread(UDPListener);
              t_listener.Name = "UDPListenerThread";
              t_listener.Start();
          }
  </code></pre>
  
  <p>The method is the following (I use a time-out on the <code>Receive</code> in order to not leave it blocked if nothing is being sent on the socket and a <code>Stop</code> is being issued):</p>
  
  <pre><code>    private void UDPListener()
      {
          int numberOfPorts = XSensAvailablePorts.Count();
          int currAttempt = 0;
          int currPort = 0;
          bool successfullAttempt = false;
          while (!successfullAttempt &amp;&amp; currAttempt &lt; numberOfPorts)
          {
              try
              {
                  currPort = Int32.Parse(XSensAvailablePorts[currAttempt]);
                  listener = new UdpClient(currPort);
                  successfullAttempt = true;
              }
              catch (Exception e)
              {
                  currAttempt++;
              }
          }
          if (successfullAttempt)
          {   
              //timeout = 2000 millis
              listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000);
              //Tried with and without, no change: listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
              statusTB.Dispatcher.BeginInvoke((Action)delegate() { statusTB.Text = "Connected on port:" + currPort; });
              groupEP = new IPEndPoint(IPAddress.Parse("143.225.85.130"), currPort);
  
              byte[] receive_byte_array;
              try
              {
                  while (!stopT_listener)
                  {
                      try
                      {
                          receive_byte_array = listener.Receive(ref groupEP);
                          if (receive_byte_array.Length == 0 || receive_byte_array == null)
                              continue;
  
                          ParseData(receive_byte_array, samplingDatagramCounter);
  
                      }
                      catch (SocketException ex)
                      {
                          if (ex.SocketErrorCode == SocketError.TimedOut)
                              continue;
                      }
  
  
                  }
              }
              catch (Exception e)
              {
                  Debug.Print(e.Message);
              }
              finally
              {
                  if (listener != null)
                  {
                      listener.Client.Shutdown(SocketShutdown.Both);
                      listener.Close();
                  }
              }
          }
          statusTB.Dispatcher.BeginInvoke((Action)delegate() { statusTB.Text = "Not Connected"; });
          return;
      }
  </code></pre>
  
  <p>I order the thread / socket to stop with this method:</p>
  
  <pre><code>private void closeSocketButton_Click(object sender, RoutedEventArgs e)
          {
              stopT_listener = true;
              closeSocketButton.IsEnabled = false;
              startSocketButton.IsEnabled = true;
              t_listener.Join();
              if (listener.Client != null)
              {
                  listener.Client.Shutdown(SocketShutdown.Both);
                  listener.Close();
              }
              if (t_listener.IsAlive)
              {
                  t_listener.Abort();
                  statusTB.Text = "Aborted";
              }
              else
                  statusTB.Text = "Not Connected";
          }
  </code></pre>
  
  <p>Despite checking in debug that the socket has been closed, if I retry to connect to the same port, I am unable to do so because it raises a <code>SocketException</code> saying that only one usage of port/address is normally permitted.</p>