private async Task<NetworkStream> GetStreamAsync()
{
//using a semaphore here to allow async waiting rather than blocking locks
using (await _clientLock.LockAsync(_disposeToken.Token).ConfigureAwait(false))
{
if ((_client == null || _client.Connected == false) && !_disposeToken.IsCancellationRequested)
{
_client = await ReEstablishConnectionAsync().ConfigureAwait(false);
}
return _client == null ? null : _client.GetStream();
}
}