protected void ProcessConnect(Socket socket, object state, SocketAsyncEventArgs e)
{
if (e != null && e.SocketError != SocketError.Success)
{
m_InConnecting = false;
OnError(new SocketException((int)e.SocketError));
e.Dispose();
return;
}
if (socket == null)
{
m_InConnecting = false;
OnError(new SocketException((int)SocketError.ConnectionAborted));
return;
}
//To walk around a MonoTouch's issue
//one user reported in some cases the e.SocketError = SocketError.Succes but the socket is not connected in MonoTouch
if (!socket.Connected)
{
m_InConnecting = false;
#if SILVERLIGHT || NETFX_CORE
var socketError = SocketError.ConnectionReset;
#else
var socketError = (SocketError)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
#endif
OnError(new SocketException((int)socketError));
return;
}
if (e == null)
e = new SocketAsyncEventArgs();
e.Completed += SocketEventArgsCompleted;
Client = socket;
m_InConnecting = false;
#if !SILVERLIGHT
try
{
// mono may throw an exception here
LocalEndPoint = socket.LocalEndPoint;
}
catch
{
}
#endif
if (e.RemoteEndPoint != null)
{
HostName = GetHostOfEndPoint(e.RemoteEndPoint);
}
else
{
try
{
HostName = GetHostOfEndPoint(socket.RemoteEndPoint);
}
catch
{
}
}
#if !SILVERLIGHT && !NETFX_CORE
try
{
//Set keep alive
Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
}
catch
{
}
#endif
OnGetSocket(e);
}