private void EndHandshake(IAsyncResult asyncResult)
{
InternalAsyncResult handshakeAsyncResult = asyncResult as InternalAsyncResult;
InternalAsyncResult readwriteAsyncResult = asyncResult.AsyncState as InternalAsyncResult;
if (!handshakeAsyncResult.IsCompleted)
{
handshakeAsyncResult.AsyncWaitHandle.WaitOne(WaitTimeOut, false);
}
if (handshakeAsyncResult.CompletedWithError)
{
// if there's a handshake error, pass it to the read asyncresult instance
readwriteAsyncResult.SetComplete(handshakeAsyncResult.AsyncException);
return;
}
if (readwriteAsyncResult.ContinueAfterHandshake)
{
// We should continue the read/write operation since the handshake is complete
if (readwriteAsyncResult.IsWriteOperation)
{
InternalBeginWrite(readwriteAsyncResult);
}
else
{
InternalBeginRead(readwriteAsyncResult);
}
}
else
{
// If we aren't continuing, we're done
readwriteAsyncResult.SetComplete();
}
}