title: UdpClient send and receive async on same port
body: <p>I am trying to send data using UdpClient to a piece of hardware that then sends a "response" on the same port.</p>
  
  <p>Obviously I first ran into the problem that everyone else trying to do the same thing had and realised I had to do it using 2 UdpClients.</p>
  
  <p>I have seen a few examples but nothing that would quite fit my needs.</p>
  
  <p>I came up with the following solution that seems to do the trick, but I am looking for some sort of validation from gurus out there.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public async Task&lt; byte[] &gt; ReceiveAsync( byte[] datagram, CancellationToken cancellationToken )
  {
      byte[] buffer = null;
  
      var tasks = new List&lt; Task &gt;
      {
          Task.Run( async () =&gt;
          {
              using ( var client = new UdpClient( 5555 ) )
              {
                  client.Client.ReceiveTimeout = 200;
                  var receiveDatagram = await client.ReceiveAsync();
                  buffer = receiveDatagram.Buffer;
              }
          }, cancellationToken ),
          Task.Run( async () =&gt;
          {
              using ( var client = new UdpClient() )
              {
                  client.Connect( "10.0.0.50", 5555 );
                  await client.SendAsync( datagram, datagram.Length );
              }
          }, cancellationToken )
      };
  
      await Task.WhenAll( tasks.ToArray() );
      return buffer;
  }
  </code></pre>
  
  <p>Does this seem to be correct or is there a better way?</p>
  
  <p>Thanks.</p>