protected override void OnListen()

in src/Transport/TcpTransportListener.cs [53:109]


        protected override void OnListen()
        {
            string listenHost = this.transportSettings.Host;
            Fx.Assert(listenHost != null, "Host cannot be null!");
            List<IPAddress> addresses = new List<IPAddress>();
            IPAddress ipAddress;

            // TODO: Fix this code to listen on Any address for FQDN pointing to the local host machine.
            if (listenHost.Equals(string.Empty))
            {
                addresses.AddRange(Dns.GetHostAddressesAsync(listenHost).Result);
            }
            else if (listenHost.Equals("localhost", StringComparison.OrdinalIgnoreCase) ||
                listenHost.Equals(Environment.GetEnvironmentVariable("COMPUTERNAME"), StringComparison.OrdinalIgnoreCase) ||
                listenHost.Equals(Dns.GetHostEntryAsync(string.Empty).Result.HostName, StringComparison.OrdinalIgnoreCase))
            {
                if (Socket.OSSupportsIPv4)
                {
                    addresses.Add(IPAddress.Any);
                }

                if (Socket.OSSupportsIPv6)
                {
                    addresses.Add(IPAddress.IPv6Any);
                }
            }
            else if (IPAddress.TryParse(listenHost, out ipAddress))
            {
                addresses.Add(ipAddress);
            }
            else
            {
                addresses.AddRange(Dns.GetHostAddressesAsync(this.transportSettings.Host).Result);
            }

            if (addresses.Count == 0)
            {
                throw new InvalidOperationException(AmqpResources.GetString(AmqpResources.AmqpNoValidAddressForHost, this.transportSettings.Host));
            }

            this.listenSockets = new Socket[addresses.Count];
            for (int i = 0; i < addresses.Count; ++i)
            {
                this.listenSockets[i] = new Socket(addresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
                this.listenSockets[i].SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                this.listenSockets[i].Bind(new IPEndPoint(addresses[i], this.transportSettings.Port));
                this.listenSockets[i].Listen(this.transportSettings.TcpBacklog);

                for (int j = 0; j < this.transportSettings.ListenerAcceptorCount; ++j)
                {
                    SocketAsyncEventArgs listenEventArgs = new SocketAsyncEventArgs();
                    listenEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(this.OnAcceptComplete);
                    listenEventArgs.UserToken = this.listenSockets[i];
                    ActionItem.Schedule(this.acceptTransportLoop, listenEventArgs);
                }
            }
        }