Caucho.IIS.Server.Connect C# (CSharp) Method

Connect() private method

private Connect ( ) : HmuxConnection
return HmuxConnection
        private HmuxConnection Connect()
        {
            lock (this) {
            if (_maxConnections <= _activeCount + _startingCount)
              return null;

            _startingCount++;
              }

              HmuxConnection connection = null;
              try {
            Socket socket = new Socket(_address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            IAsyncResult asyncResult = socket.BeginConnect(_address, _port, null, socket);
            asyncResult.AsyncWaitHandle.WaitOne(_loadBalanceConnectTimeout);

            if (!socket.Connected)
            {
              try
              {
            socket.Close();
              }
              catch (Exception e)
              {
            if (_log.IsLoggable(EventLogEntryType.Error))
            {
              String message = String.Format("Closing of Socket {0}:{1} failed due to: {2}", _address, _port, e.Message);
              _log.Error(message);
            }
              }
              finally
              {
            throw new SocketException(10060);
              }
            }

            socket.SendTimeout = _socketTimeout;
            socket.ReceiveTimeout = _socketTimeout;

            String traceId;
            if (_isDebug) {
              int i = _traceId++;
              traceId = i.ToString();
            } else {
              traceId = socket.Handle.ToInt32().ToString();
            }

            connection = new HmuxConnection(socket, this, _serverInternalId, traceId);

            lock (this) {
              _activeCount++;
            }

            Trace.TraceInformation("Connect '{0}'", connection);

            return connection;
              } catch (SocketException e) {
            String message = String.Format("Socket connection to {0}:{1} timed out on load-balance-connect-timeout {2} due to: {3}({4})", _address, _port, _loadBalanceConnectTimeout, e.Message, e.ErrorCode);
            if (_log.IsLoggable(EventLogEntryType.Error))
            {
              _log.Error(message);
            }

            Trace.TraceInformation(message);
              } catch (Exception e) {
            String message = String.Format("Can't create HmuxChannel to '{0}:{1}' due to: {2} \t {3}", _address, _port, e.Message, e.StackTrace);
            if (_log.IsLoggable(EventLogEntryType.Error))
              _log.Error(message);

            Trace.TraceError(message);
              } finally {
            lock (this) {
              _startingCount--;
            }

            if (connection == null)
              FailConnect();
              }

              return null;
        }