/// <summary>
        /// Enqueue changes.
        /// </summary>
        /// <param name="entries">Changes to process.</param>
        public void AddToQueue(List <SearchResultEntry> entries)
        {
            if ((entries != null) && entries.Any())
            {
                lock (mQueueLock)
                {
                    // Add items to query
                    Queue.AddRange(entries);

                    // Sort query by uSNChanged and process users first
                    Queue.Sort((a, b) =>
                    {
                        if (LdapHelper.IsUser(a) ^ LdapHelper.IsUser(b))
                        {
                            return(LdapHelper.IsUser(a) ? -1 : 1);
                        }

                        return(LdapHelper.GetUsnChanged(a).CompareTo(LdapHelper.GetUsnChanged(b)));
                    });

                    // Start sending changes
                    if (DispatchAllowed && !DispatchRunning)
                    {
                        DispatchRunning = true;
                        Task.Factory.StartNew(SendChanges, TaskCreationOptions.LongRunning);
                    }
                }
            }
        }
        /// <summary>
        /// Send changes to CMS one-by-one.
        /// </summary>
        private void SendChanges()
        {
            // Re-enumerate queue in each iteration
            while (Queue.Any() && DispatchAllowed)
            {
                lock (mQueueLock)
                {
                    try
                    {
                        if (Replica == null)
                        {
                            LoadDirecotryReplica();
                        }

                        // Process first entry
                        var entry = Queue.First();

                        if (entry == null)
                        {
                            // Remove all nulls
                            Queue.RemoveAll(e => e == null);
                            continue;
                        }

                        // Handle incoming change
                        if (LdapHelper.IsUser(entry))
                        {
                            HandleUser(entry);
                        }
                        else if (LdapHelper.IsGroup(entry))
                        {
                            HandleGroup(entry);
                        }

                        Queue.Remove(entry);

                        // Set actual uSNChanged attribute
                        long newUsn = LdapHelper.GetUsnChanged(entry);
                        if ((Replica != null) && (Replica.HighestUsnChanged < newUsn))
                        {
                            Replica.HighestUsnChanged = newUsn;
                        }

                        SaveDirecotryReplica();
                    }
                    catch (Exception ex)
                    {
                        LogError("Exception occurred when processing object.", ex);
                    }
                }
            }

            DispatchRunning = false;
        }