Apache.NMS.ActiveMQ.Transport.Discovery.Multicast.MulticastDiscoveryAgent.TryToConnectSocket C# (CSharp) Method

TryToConnectSocket() private method

private TryToConnectSocket ( ) : bool
return bool
        private bool TryToConnectSocket()
        {
            bool hasSucceeded = false;

            try
            {
                multicastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                endPoint = new IPEndPoint(IPAddress.Any, discoveryUri.Port);

                //We have to allow reuse in the multicast socket. Otherwise, we would be unable to use multiple clients on the same machine.
                multicastSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                multicastSocket.Bind(endPoint);

                IPAddress ipaddress;

                if(!TcpTransportFactory.TryParseIPAddress(discoveryUri.Host, out ipaddress))
                {
                    ipaddress = TcpTransportFactory.GetIPAddress(discoveryUri.Host, AddressFamily.InterNetwork);
                    if(null == ipaddress)
                    {
                        throw new NMSConnectionException("Invalid host address.");
                    }
                }

                multicastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                                 new MulticastOption(ipaddress, IPAddress.Any));
            #if !NETCF
                multicastSocket.ReceiveTimeout = SOCKET_TIMEOUT_MILLISECONDS;
            #endif
                hasSucceeded = true;
            }
            catch(SocketException)
            {
            }

            return hasSucceeded;
        }