//This gets called when the listener receives a request
        private void ProcessRequest(HttpListenerContext context)
        {
            var isLocal = System.Net.IPAddress.IsLoopback(context.Request.RemoteEndPoint.Address) ||
                          context.Request.LocalEndPoint == context.Request.RemoteEndPoint;

            if (isLocal)
            {
                Log.To.Listener.I(TAG, "Received new {0} local connection",
                                  _usesTLS ? "secure" : "plain");
            }
            else
            {
                Log.To.Listener.I(TAG, "Received new {0} remote connection from {1}",
                                  _usesTLS ? "secure" : "plain", context.Request.RemoteEndPoint.Address);
            }

            var getContext = Task.Factory.FromAsync <HttpListenerContext>(_listener.BeginGetContext, _listener.EndGetContext, null);

            getContext.ContinueWith(t => ProcessRequest(t.Result));

            var internalContext = new CouchbaseListenerTcpContext(context.Request, context.Response, _manager);

            internalContext.IsLoopbackRequest = isLocal;
            var uriBuilder = new UriBuilder(_usesTLS ? "https" : "http", context.Request.LocalEndPoint.Address.ToString(),
                                            context.Request.LocalEndPoint.Port);

            uriBuilder.UserName    = context.User != null && context.User.Identity != null ? context.User.Identity.Name : null;
            internalContext.Sender = uriBuilder.Uri;
            Log.To.Listener.D(TAG, "Sender set to {0}", internalContext.Sender);
            _router.HandleRequest(internalContext);
        }
        public void TestListenerRequestsAreExternal()
        {
            var fakeListener = new CouchbaseLiteMockTcpListener(59842);
            fakeListener.ContextGenerator = context =>
            {
                var internalContext = new CouchbaseListenerTcpContext(context.Request, context.Response, manager);
                var uriBuilder = new UriBuilder("http", context.Request.LocalEndPoint.Address.ToString(),
                    context.Request.LocalEndPoint.Port);
                uriBuilder.UserName = context.User != null && context.User.Identity != null ? context.User.Identity.Name : null;
                internalContext.Sender = uriBuilder.Uri;
                return internalContext;
            };
            _listenerDBUri = new Uri("http://127.0.0.1:59842/" + LISTENER_DB_NAME);
            fakeListener.Start();
            try {
                CreateDocs(database, false);
                var repl = CreateReplication(database, true);
                var allChangesExternal = true;
                _listenerDB.Changed += (sender, e) => 
                {
                    allChangesExternal = allChangesExternal && e.IsExternal;
                };

                RunReplication(repl);
                VerifyDocs(_listenerDB, false);
                Assert.IsTrue(allChangesExternal);
            } finally {
                fakeListener.Stop();
            }
        }
        //This gets called when the listener receives a request
        private void ProcessRequest (HttpListenerContext context)
        {
            var isLocal = System.Net.IPAddress.IsLoopback(context.Request.RemoteEndPoint.Address) ||
                          context.Request.LocalEndPoint == context.Request.RemoteEndPoint;

            Log.To.Listener.I(TAG, "Received new {0} {1} connection", 
                _usesTLS ? "secure" : "plain", isLocal ? "local" : "remote");
            var getContext = Task.Factory.FromAsync<HttpListenerContext>(_listener.BeginGetContext, _listener.EndGetContext, null);
            getContext.ContinueWith(t => ProcessRequest(t.Result));

            var internalContext = new CouchbaseListenerTcpContext(context.Request, context.Response, _manager);
            internalContext.IsLoopbackRequest = isLocal;
            var uriBuilder = new UriBuilder(_usesTLS ? "https" : "http", context.Request.LocalEndPoint.Address.ToString(),
                                 context.Request.LocalEndPoint.Port);
            uriBuilder.UserName = context.User != null && context.User.Identity != null ? context.User.Identity.Name : null;
            internalContext.Sender = uriBuilder.Uri;
            Log.To.Listener.D(TAG, "Sender set to {0}", internalContext.Sender);
            _router.HandleRequest(internalContext);
        }