TcpClient; NetworkStream; ReadAsync; C#
<p>Please excuse my lack of knowledge regarding Tasks and Async.</p>

<p>Using the TcpClient class I am creating a connection with an available server:</p>

<pre><code>void async RunClientAsync()
{
    TcpClient client = new TcpClient();
    try
    {
        await client.ConnectAsync(IPAddress.Parse("1.1.1.1"), 8889);
        Task.Start(() =&gt; ReadClientAsync(client));
    }
    catch (Exception ex)
    {
        HandleException(ex);
    }
}

// -----

void async ReadClientAsync(TcpClient client)
{
    byte[] bf = new byte[2048];
    try
    {
        while(true)
        {
            int br = await client.NetworkStream().ReadAsync();
            if (br &gt; 0) 
            {
                HandleInboundData(bf, br);
            }
        }
    }
    catch (Exception ex)
    {
        HandleException(ex);
    }
}
</code></pre>

<p>The helper methods HandleException(Exception ex) and HandleInboundData(byte[] buffer, int length) will perform the assumed tasks.</p>

<p>The connection to the server will be in perpetuity and the data received from the server will be of unknown length and frequency, the idea being to throw a task out there that receives and processes the inbound data only when data is available.  </p>

<p>ReadClientAsync(TcpClient client) is an obvious fail because ReadAsync will always return 0 bytes if there is no data available.  </p>

<p>How should I approach writing ReadClientAsync using async / task to prevent the busy-looping situation?  I've used BeginRead / EndRead in these situations before, which has worked fine.  Would that be the solution in this particular case?</p>

<p>Thank you,</p>