/// <summary>
        /// Creates a <see cref="SnapNetworkClient"/>
        /// </summary>
        /// <param name="settings">The config to use for the client</param>
        /// <param name="credentials">The network credentials to use.
        /// If left null, the computers current credentials are use.</param>
        /// <param name="useSsl">Specifies if ssl encryption is desired for the connection.</param>
        public SnapNetworkClient(SnapNetworkClientSettings settings, SecureStreamClientBase credentials = null, bool useSsl = false)
        {
            if (credentials == null)
            {
                if (settings.UseIntegratedSecurity)
                {
                    credentials = new SecureStreamClientIntegratedSecurity();
                }
                else
                {
                    credentials = new SecureStreamClientDefault();
                }
            }

            IPAddress ip;

            if (!IPAddress.TryParse(settings.ServerNameOrIp, out ip))
            {
                ip = Dns.GetHostAddresses(settings.ServerNameOrIp)[0];
            }

            IPEndPoint server = new IPEndPoint(ip, settings.NetworkPort);

            m_client = new TcpClient(ip.AddressFamily);
            m_client.Connect(server);

            m_client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            Initialize(new NetworkStream(m_client.Client), credentials, useSsl);
        }
        /// <summary>
        /// Creates a <see cref="SnapStreamingClient"/>
        /// </summary>
        /// <param name="stream">The config to use for the client</param>
        /// <param name="credentials">Authenticates using the supplied user credentials.</param>
        /// <param name="useSsl">specifies if a ssl connection is desired.</param>
        protected void Initialize(Stream stream, SecureStreamClientBase credentials, bool useSsl)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (credentials == null)
            {
                throw new ArgumentNullException("credentials");
            }

            m_credentials = credentials;
            m_rawStream   = stream;
            m_rawStream.Write(0x2BA517361121L);
            m_rawStream.Write(useSsl); //UseSSL

            var command = (ServerResponse)m_rawStream.ReadNextByte();

            switch (command)
            {
            case ServerResponse.UnknownProtocol:
                throw new Exception("Client and server cannot agree on a protocol, this is commonly because they are running incompatible versions.");

            case ServerResponse.KnownProtocol:
                break;

            default:
                throw new Exception("Unknown server response: " + command.ToString());
            }

            useSsl = m_rawStream.ReadBoolean();

            if (!m_credentials.TryAuthenticate(m_rawStream, useSsl, out m_secureStream))
            {
                throw new Exception("Authentication Failed");
            }

            m_stream = new RemoteBinaryStream(m_secureStream);

            command = (ServerResponse)m_stream.ReadUInt8();
            switch (command)
            {
            case ServerResponse.UnhandledException:
                string exception = m_stream.ReadString();
                throw new Exception("Server UnhandledExcetion: \n" + exception);

            case ServerResponse.UnknownProtocol:
                throw new Exception("Client and server cannot agree on a protocol, this is commonly because they are running incompatible versions.");

            case ServerResponse.ConnectedToRoot:
                break;

            default:
                throw new Exception("Unknown server response: " + command.ToString());
            }

            RefreshDatabaseInfo();
        }
 /// <summary>
 /// Creates a <see cref="SnapStreamingClient"/>
 /// </summary>
 /// <param name="stream">The config to use for the client</param>
 /// <param name="credentials">Authenticates using the supplied user credentials.</param>
 /// <param name="useSsl">specifies if a ssl connection is desired.</param>
 public SnapStreamingClient(Stream stream, SecureStreamClientBase credentials, bool useSsl)
 {
     Initialize(stream, credentials, useSsl);
 }