protected Socket Connect()

in src/main/csharp/Transport/Tcp/TcpTransportFactory.cs [281:344]


		protected Socket Connect(string host, int port)
		{
			Socket socket = null;
			IPAddress ipaddress;

			try
			{
				if(TryParseIPAddress(host, out ipaddress))
				{
					socket = ConnectSocket(ipaddress, port);
				}
				else
				{
					// Looping through the AddressList allows different type of connections to be tried
					// (IPv6, IPv4 and whatever else may be available).
					IPHostEntry hostEntry = GetIPHostEntry(host);

					if(null != hostEntry)
					{
						// Prefer IPv6 first.
						ipaddress = GetIPAddress(hostEntry, AddressFamily.InterNetworkV6);
						socket = ConnectSocket(ipaddress, port);
						if(null == socket)
						{
							// Try IPv4 next.
							ipaddress = GetIPAddress(hostEntry, AddressFamily.InterNetwork);
							socket = ConnectSocket(ipaddress, port);
							if(null == socket)
							{
								// Try whatever else there is.
								foreach(IPAddress address in hostEntry.AddressList)
								{
									if(AddressFamily.InterNetworkV6 == address.AddressFamily
										|| AddressFamily.InterNetwork == address.AddressFamily)
									{
										// Already tried these protocols.
										continue;
									}

									socket = ConnectSocket(address, port);
									if(null != socket)
									{
										ipaddress = address;
										break;
									}
								}
							}
						}
					}
				}

				if(null == socket)
				{
					throw new SocketException();
				}
			}
			catch(Exception ex)
			{
				throw new NMSConnectionException(String.Format("Error connecting to {0}:{1}.", host, port), ex);
			}

			Tracer.DebugFormat("Connected to {0}:{1} using {2} protocol.", host, port, ipaddress.AddressFamily.ToString());
			return socket;
		}