protected bool Equals(ConnectionKey other)
 {
     return string.Equals(m_host, other.m_host) && string.Equals(m_port, other.m_port);
 }
        private void OnConnectionShutdown(IConnection connection, ShutdownEventArgs reason)
        {
            ConnectionKey key = new ConnectionKey(connection.Endpoint.HostName, connection.Endpoint.Port);

            lock (m_connections)
            {
                if (m_connections.ContainsKey(key))
                {
                    m_connections.Remove(key);
                }

                connection.ConnectionShutdown -= OnConnectionShutdown;
            }
        }
        private IConnection OpenConnection(ConnectionKey key, RabbitMQUri uri, IProtocol protocol)
        {
            ConnectionFactory connFactory = new ConnectionFactory
                {
                    HostName = key.Host,
                    Port = key.Port,
                    Protocol = protocol
                };

            if (uri.Username != null)
            {
                connFactory.UserName = uri.Username;
            }
            if (uri.Password != null)
            {
                connFactory.Password = uri.Password;
            }
            if (uri.VirtualHost != null)
            {
                connFactory.VirtualHost = uri.VirtualHost;
            }

            IConnection connection = connFactory.CreateConnection();

            connection.ConnectionShutdown += OnConnectionShutdown;

            return connection;
        }
        public IModel OpenModel(RabbitMQUri uri, IProtocol protocol, TimeSpan timeout)
        {
            string host = uri.Host;
            int port = uri.Port.HasValue ? uri.Port.Value : protocol.DefaultPort;

            ConnectionKey key = new ConnectionKey(host, port);

            lock (m_connections)
            {
                IConnection connection;

                if (m_connections.ContainsKey(key))
                {
                    connection = m_connections[key];
                }
                else
                {
                    connection = OpenConnection(key, uri, protocol);

                    m_connections.Add(key, connection);
                }

                IModel model = connection.CreateModel();

                connection.AutoClose = true;

                return model;
            }
        }