/// <summary> /// Connects to the specified host. If the hostname resolves to more than one IP address, /// all IP addresses will be tried for connection, until one of them connects. /// </summary> /// <param name="host">Host name or IP address.</param> /// <param name="port">Port to connect.</param> /// <param name="ssl">Specifies if connects to SSL end point.</param> /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this method is accessed.</exception> /// <exception cref="InvalidOperationException">Is raised when client is already connected.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> /// <exception cref="ArgumentOutOfRangeException">Is raised when port isnot in valid range.</exception> public void Connect(string host, int port, bool ssl) { ThrowIfObjectDisposed(); ThrowIfConnected(); AssertUtil.ArgumentNotEmpty(host, nameof(host)); AssertUtil.AssertNetworkPort(port, nameof(port)); var ips = NameResolver.GetHostAddresses(host); for (int i = 0; i < ips.Length; i++) { try { Connect(null, new IPEndPoint(ips[i], port), ssl); break; } catch (Exception ex) { if (this.IsConnected) { throw ex; } // Connect failed for specified IP address, // if there are some more IPs left, try next, otherwise forward exception. else if (i == (ips.Length - 1)) { throw ex; } } } }