private void Worker(object state)
        {
            if (log.IsDebugEnabled) log.Debug("Started working.");

            while (!this.stopEvent.WaitOne(0))
            {
                if (this.requestFactory == null)
                    this.requestFactory = this.CreateRequestFactory();

                // false means that the url was not responding or failed while reading
                this.statusPool = this.urls.ToDictionary(u => u, u => true);
                this.urlIndex = 0;

                // this will quit when all nodes go down or we're stopped externally
                this.ProcessPool();

                // pool fail
                if (!this.stopEvent.WaitOne(0))
                {
                    if (log.IsWarnEnabled) log.Warn("All nodes are dead, sleeping for a while.");

                    this.Trigger(null);

                    this.SleepUntil(this.DeadTimeout);

                    // recreate the client after failure
                    this.AbortRequests();
                    if (this.requestFactory != null)
                    {
                        this.requestFactory.Dispose();
                        this.requestFactory = null;
                    }
                }
            }
        }
        private static Uri ResolveBucketUri(WebClientWithTimeout client, Uri root, string bucketName)
        {
            try
            {
                var bucket = ConfigHelper.ResolveBucket(client, root, bucketName);
                if (bucket == null)
                    return null;

                if (String.IsNullOrEmpty(bucket.streamingUri))
                {
                    log.ErrorFormat("Url {0} for bucket {1} returned a config with no streamingUri", root, bucketName);
                    return null;
                }

                return new Uri(root, bucket.streamingUri);
            }
            catch (Exception e)
            {
                log.Error("Error resolving streaming uri: " + root, e);

                return null;
            }
        }
        protected void Dispose()
        {
            AppDomain.CurrentDomain.DomainUnload -= CurrentDomain_DomainUnload;

            this.AbortRequests();

            if (this.requestFactory != null)
            {
                using (this.requestFactory)
                    this.requestFactory.CancelAsync();

                this.requestFactory = null;
            }
        }
        private Uri ResolveBucketUri(WebClientWithTimeout client, Uri uri)
        {
            try
            {
                var helper = new ConfigHelper(client);
                var bucket = helper.ResolveBucket(uri, this.bucketName);
                if (bucket == null)
                    return null;

                var streamingUri = bucket.streamingUri;

                var node = bucket.nodes.FirstOrDefault();

                // beta 2 hack, will be phased out after b3 is released for a while
                if (node != null && node.version == "1.6.0beta2")
                    streamingUri = streamingUri.Replace("/bucketsStreaming/", "/bucketsStreamingConfig/");

                return new Uri(uri, streamingUri);
            }
            catch (Exception e)
            {
                log.Error("Error resolving streaming uri", e);

                return null;
            }
        }
        protected void Dispose()
        {
            AppDomain.CurrentDomain.DomainUnload -= CurrentDomain_DomainUnload;

            this.CleanupRequests();

            if (this.client != null)
            {
                using (this.client)
                    this.client.CancelAsync();

                this.client = null;
            }
        }
        private void Worker(object state)
        {
            if (log.IsDebugEnabled) log.Debug("Started working.");

            while (this.stopCounter == 0)
            {
                if (this.client == null)
                    this.client = this.CreateClient();

                // false means that the url was not responding or failed while reading
                this.statusPool = this.urls.ToDictionary(u => u, u => true);
                this.urlIndex = 0;

                // this will quit when all nodes go down or we're stopped externally
                this.ProcessPool();

                // pool fail
                if (this.stopCounter == 0)
                {
                    if (log.IsWarnEnabled) log.Warn("All nodes are dead, sleeping for a while.");

                    this.Trigger(null);

                    DateTime now = DateTime.UtcNow;

                    var waitUntil = this.DeadTimeout;
                    while (this.stopCounter == 0
                            && (DateTime.UtcNow - now).TotalMilliseconds < waitUntil)
                    {
                        Thread.Sleep(100);
                    }

                    // recreate the client after failure
                    this.CleanupRequests();
                    this.client.Dispose();
                    this.client = null;
                }
            }
        }
        protected void Dispose()
        {
            this.CleanupRequests();

            if (this.client != null)
            {
                using (this.client)
                    this.client.CancelAsync();

                this.client = null;
            }
        }