void Connect (HttpWebRequest request)
{
lock (socketLock) {
if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
// Take the chunked stream to the expected state (State.None)
if (CanReuse () && CompleteChunkedRead ()) {
reused = true;
return;
}
}
reused = false;
if (socket != null) {
socket.Close();
socket = null;
}
chunkStream = null;
IPHostEntry hostEntry = sPoint.HostEntry;
if (hostEntry == null) {
#if MONOTOUCH
if (start_wwan != null) {
start_wwan.Invoke (null, new object [1] { sPoint.Address });
hostEntry = sPoint.HostEntry;
}
if (hostEntry == null) {
#endif
status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
WebExceptionStatus.NameResolutionFailure;
return;
#if MONOTOUCH
}
#endif
}
//WebConnectionData data = Data;
foreach (IPAddress address in hostEntry.AddressList) {
socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remote = new IPEndPoint (address, sPoint.Address.Port);
socket.NoDelay = !sPoint.UseNagleAlgorithm;
try {
sPoint.KeepAliveSetup (socket);
} catch {
// Ignore. Not supported in all platforms.
}
if (!sPoint.CallEndPointDelegate (socket, remote)) {
socket.Close ();
socket = null;
status = WebExceptionStatus.ConnectFailure;
} else {
try {
if (request.Aborted)
return;
socket.Connect (remote);
status = WebExceptionStatus.Success;
break;
} catch (ThreadAbortException) {
// program exiting...
Socket s = socket;
socket = null;
if (s != null)
s.Close ();
return;
} catch (ObjectDisposedException) {
// socket closed from another thread
return;
} catch (Exception exc) {
Socket s = socket;
socket = null;
if (s != null)
s.Close ();
if (!request.Aborted)
status = WebExceptionStatus.ConnectFailure;
connect_exception = exc;
}
}
}
}
}