title: Reuse UdpClient vs Disposing it
body: <p>I am creating a chat application where users are behind a router (NAT). So the main problem is sending those clients a message. The server has no problem receiving messages.</p>
  
  <h2>This is my protocol (rules):</h2>
  
  <ol>
  <li><code>UdpServer</code> listens for a udp packet from <code>client A</code></li>
  <li><code>Server</code> receives packet from <code>client A</code> and replies with a packet to open the NAT</li>
  <li><p><code>Server</code> now becomes a client (server constantly sends packets to <code>client A</code> and the client replies to the server notifying it that it got his message) <strong>I reverse the client and server now that the NAT is open</strong>. This is because <code>client A</code> can always send a message to the server through tcp.</p></li>
  <li><p>If <code>Server</code> sees that <code>client A</code> does not reply back to the packets that its constantly sending it marks <code>client A</code> as disconnected and stops sending packets.</p></li>
  </ol>
  
  <h2>This is the code</h2>
  
  <pre><code>// class that holds infor about client
  class ClientBehindNat
  {
       public IPEndPoint EndpointListening;
       public string Id;
  }
  </code></pre>
  
  <p>thread that listens for new clients (connections):</p>
  
  <pre><code>while(true)
  {
      IPEndPoint ep = new IPEndPoint(IPAddress.Any, 1234);
      // create a new udpServer
      using(var udpServer = new UdpClient())
      {
          // allow reusing port 1234
          udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
          // wait to receive data
          var data = udpServer.Receive(ref ep);
  
          // analyze data make sure it is reliable....
  
          // maybe data is a reply from a client 
          if(data.IsReply())
          {
               // mark that client replied
               // ....
               continue;
          }
          else
          {
              // save new client
              ConnectedClients.Add(new ClientBehindNat(){EndpointListening=ep});
          }
  
          udpServer.Connect(ep);
          udpServer.Send( // data notifying client that we got his message)
  
      }
  }
  </code></pre>
  
  <p>thread that constantly sends udp packets to client in order to keep nat open</p>
  
  <pre><code>// send packets every 15 seconds in order to leave nat open
  timer.elapsed += (a,b)=&gt; {
  
      foreach (var c in connectedClients)
      {
          // create a copy of udp server so that client can get our response
          using (var copySocket = new UdpClient())
          {
              // allow reusing port 1234
              copySocket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
              copySocket.Client.Bind(new IPEndPoint(IPAddress.Any, 1234));
              // log that we will send this request
              // Requests[someId] = ....
  
              // send it
              copySocket.Send(someData, someData.Length, c.EndpointListening);
  
              // first thread should be responsible for receiving this response
  
              // dispose this client                            
          }          
      }
  }
  </code></pre>
  
  <h2>This is the question</h2>
  
  <p><strong>Everything works great</strong> BUT is it bad to create a new Socket (UdpClient) per request? If I receive 100 requests I create 100 new sockets and dispose all that. When I try to reuse a udp client updating its endpoints I get an exception. I often need to send a message to <code>Client A</code> or maybe <code>Client Z</code> thats why I store its endpoint to know how to reach it. <strong>How can I reuse a udpClient updating its endpoints so that I am able to reach the client I am interested in?</strong></p>