private bool CheckEnqueueHandshakeRead(ref byte[] buffer, AsyncProtocolRequest request)
{
LazyAsyncResult lazyResult = null;
lock (this)
{
if (_lockReadState == LockPendingRead)
{
return false;
}
int lockState = Interlocked.Exchange(ref _lockReadState, LockHandshake);
if (lockState != LockRead)
{
return false;
}
if (request != null)
{
_queuedReadStateRequest = request;
return true;
}
lazyResult = new LazyAsyncResult(null, null, /*must be */ null);
_queuedReadStateRequest = lazyResult;
}
// Need to exit from lock before waiting.
lazyResult.InternalWaitForCompletion();
buffer = (byte[])lazyResult.Result;
return false;
}