public IntradomainChannel(IntradomainEndpoint endpoint,
                                  IMessageSerializer serializer,
                                  ChannelSettings channelSettings = null,
                                  IntradomainConnectionSettings connectionSettings = null,
                                  ISecurityWrapper clientSecurityWrapper           = null)
            : this(endpoint, SocketType.Client, serializer)
        {
            _connectionSettings = connectionSettings ?? IntradomainConnectionSettingsBuilder.GetDefaultSettings();
            MaxMessageLength    = channelSettings?.MaxMessageLength ?? ChannelSettings.GetDefault().MaxMessageLength;

            SecurityWrapper = clientSecurityWrapper;
        }
        internal IntradomainChannel(IntradomainSocket socket,
                                    IMessageSerializer serializer,
                                    ChannelSettings channelSettings = null,
                                    IntradomainConnectionSettings connectionSettings = null,
                                    ISecurityWrapper serverSecurityWrapper           = null)
            : this(socket, serializer)
        {
            _connectionSettings = connectionSettings ?? IntradomainConnectionSettingsBuilder.GetDefaultSettings();
            MaxMessageLength    = channelSettings?.MaxMessageLength ?? ChannelSettings.GetDefault().MaxMessageLength;

            SecurityWrapper = serverSecurityWrapper;
        }
        public bool Connect(IntradomainSocket socket, IntradomainConnectionSettings connectionSettings = null)
        {
            if (string.IsNullOrEmpty(socket.Hub))
            {
                return(false);
            }

            if (socket.Type != SocketType.Client)
            {
                throw new ArgumentException(nameof(socket));
            }

            Func <IntradomainSocket, IntradomainSocket> getSocketData;

            lock (_locker)
            {
                if (!_listeners.ContainsKey(socket.Hub))
                {
                    return(false);
                }

                getSocketData = _listeners[socket.Hub];
            }
            socket.ConnectionId = Guid.NewGuid().ToString();
            var remoteSocket = getSocketData(socket);

            if (remoteSocket == null)
            {
                return(false);
            }

            lock (_locker)
            {
                var connection = new IntradomainConnetcion(remoteSocket,
                                                           socket,
                                                           connectionSettings ?? IntradomainConnectionSettingsBuilder.GetDefaultSettings())
                {
                    ClientSocketState = IntradomainSocketState.Connected
                };

                _connections.Add(socket.ConnectionId, connection);
                _connectionCreated.Set();
                return(true);
            }
        }
        internal static IntradomainChannel <TMessage> CreateAndOpen <TMessage>(AutoResetEvent channelCreatedEvent,
                                                                               IntradomainSocket socket,
                                                                               IMessageSerializer serializer,
                                                                               ChannelSettings channelSettings = null,
                                                                               IntradomainConnectionSettings connectionSettings = null,
                                                                               ISecurityWrapper serverSecurityWrapper           = null)
            where TMessage : IMessage
        {
            var result = new IntradomainChannel <TMessage>(socket, serializer, channelSettings, connectionSettings, serverSecurityWrapper);

            EstablishConnection(result, channelCreatedEvent, socket, connectionSettings?.ConnectTimeout ?? IntradomainConnectionSettingsBuilder.GetDefaultSettings().ConnectTimeout);
            return(result);
        }