private void OnConnectComplete(object sender, SocketAsyncEventArgs e)
{
WriteOperation operation = (WriteOperation) e.UserToken;
// dual stack ConnectAsync may call in with null UserToken if
// one connection attempt timed out but the other succeeded
if (operation == null) return;
if (e.SocketError != SocketError.Success)
{
operation.Fault(StatusCodes.BadNotConnected);
return;
}
lock (DataLock)
{
try
{
// check for closed socket.
if (Socket == null)
{
operation.Fault(StatusCodes.BadSecureChannelClosed);
return;
}
// start reading messages.
Socket.ReadNextMessage();
// send the hello message.
SendHelloMessage(operation);
}
catch (Exception ex)
{
ServiceResult fault = ServiceResult.Create(
ex,
StatusCodes.BadTcpInternalError,
"An unexpected error occurred while connecting to the server.");
operation.Fault(fault);
}
}
}