internal static IPEndPoint IPEndPointFromHostPortString(string hostAndPort) { if ((hostAndPort == null) || string.IsNullOrEmpty(hostAndPort.Trim())) { return(null); } if (hostAndPort.IndexOf(';') > -1) { hostAndPort = hostAndPort.Substring(0, hostAndPort.IndexOf(';')); } try { string str; int port = 80; CrackHostAndPort(hostAndPort, out str, ref port); return(new IPEndPoint(DNSResolver.GetIPAddress(str, true), port)); } catch (Exception) { return(null); } }
private bool ConnectToHost() { string str; int port = _session.Port; string hostPort = _session.Host; Utilities.CrackHostAndPort(hostPort, out str, ref port); var remoteEp = Proxy.Gateway; if (remoteEp != null) { WasForwarded = true; } else { IPAddress address; try { address = DNSResolver.GetIPAddress(str, true); } catch { _session.Request.FailSession(0x1f6, "DNS Lookup Failed", "DNS Lookup for " + HttpUtility.HtmlEncode(str) + " failed."); return(false); } if ((port < 0) || (port > 0xffff)) { _session.Request.FailSession(0x1f6, "Invalid Request", "HTTP Request specified an invalid port number."); return(false); } remoteEp = new IPEndPoint(address, port); } try { var socket = new Socket(remoteEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true, ReceiveTimeout = 10000, SendTimeout = 10000 }; ServerSocket = socket; ServerSocket.Connect(remoteEp); return(true); } catch (Exception exception) { if (WasForwarded) { _session.Request.FailSession( 0x1f6, "Gateway Connection Failed", string.Format(CultureInfo.InvariantCulture, "Connection to Gateway failed.<BR>Exception Text: {0}", exception.Message)); } else { _session.Request.FailSession( 0x1f6, "Connection Failed", string.Format(CultureInfo.InvariantCulture, "Connection to {0} failed.<BR>Exception Text: {1}", HttpUtility.HtmlEncode(str), exception.Message)); } return(false); } }