/// <summary>
        /// Creates a response message to a GetNeighbourNodesByDistanceLocalRequest message.
        /// </summary>
        /// <param name="Request">GetNeighbourNodesByDistanceLocalRequest message for which the response is created.</param>
        /// <param name="Nodes">List of nodes in the neighborhood.</param>
        /// <returns>GetNeighbourNodesByDistanceLocalResponse message that is ready to be sent.</returns>
        public Message CreateGetNeighbourNodesByDistanceLocalResponse(Message Request, IEnumerable <NodeInfo> Nodes)
        {
            GetNeighbourNodesByDistanceResponse getNeighbourNodesByDistanceResponse = new GetNeighbourNodesByDistanceResponse();

            getNeighbourNodesByDistanceResponse.Nodes.AddRange(Nodes);

            Message res = CreateLocalServiceOkResponse(Request);

            res.Response.LocalService.GetNeighbourNodes = getNeighbourNodesByDistanceResponse;

            return(res);
        }
        /// <summary>
        /// Processes GetNeighbourNodesByDistanceResponse message received from LOC server.
        /// <para>This message contains information about proximity server's neighbors, with which it should share its activity database.</para>
        /// </summary>
        /// <param name="ResponseMessage">Full response message.</param>
        /// <param name="IsInitialization">true if the response was received to the request during the LOC initialization, false if it was received to the refresh request after the initialization.</param>
        /// <returns>true if the connection to the LOC server should remain open, false if it should be closed.</returns>
        public async Task <bool> ProcessMessageGetNeighbourNodesByDistanceResponseAsync(LocProtocolMessage ResponseMessage, bool IsInitialization)
        {
            log.Trace("(IsInitialization:{0})", IsInitialization);

            bool res = false;
            bool signalActionProcessor = false;

            GetNeighbourNodesByDistanceResponse getNeighbourNodesByDistanceResponse = ResponseMessage.Response.LocalService.GetNeighbourNodes;

            if (getNeighbourNodesByDistanceResponse.Nodes.Count > 0)
            {
                using (UnitOfWork unitOfWork = new UnitOfWork())
                {
                    DatabaseLock[] lockObjects = new DatabaseLock[] { UnitOfWork.NeighborLock, UnitOfWork.NeighborhoodActionLock };
                    using (IDbContextTransaction transaction = await unitOfWork.BeginTransactionWithLockAsync(lockObjects))
                    {
                        bool success = false;
                        bool saveDb  = false;
                        try
                        {
                            int neighborhoodSize = await unitOfWork.NeighborRepository.CountAsync();

                            foreach (NodeInfo nodeInfo in getNeighbourNodesByDistanceResponse.Nodes)
                            {
                                // Check whether a proximity server is running on this node.
                                // If not, it is not interesting for us at all, skip it.
                                int    proximityServerPort;
                                byte[] proximityServerId;
                                if (!HasProximityServerService(nodeInfo, out proximityServerPort, out proximityServerId))
                                {
                                    continue;
                                }

                                NodeContact contact   = nodeInfo.Contact;
                                byte[]      ipBytes   = contact.IpAddress.ToByteArray();
                                IPAddress   ipAddress = new IPAddress(ipBytes);

                                int latitude  = nodeInfo.Location.Latitude;
                                int longitude = nodeInfo.Location.Longitude;

                                AddOrChangeNeighborResult addChangeRes = await AddOrChangeNeighborAsync(unitOfWork, proximityServerId, ipAddress, proximityServerPort, latitude, longitude, neighborhoodSize);

                                neighborhoodSize = addChangeRes.NeighborhoodSize;

                                if (addChangeRes.SaveDb)
                                {
                                    saveDb = true;
                                }

                                if (addChangeRes.SignalActionProcessor)
                                {
                                    signalActionProcessor = true;
                                }

                                // We do ignore errors here and just continue processing another item from the list.
                            }

                            if (saveDb)
                            {
                                await unitOfWork.SaveThrowAsync();

                                transaction.Commit();
                            }
                            success = true;
                            res     = true;
                        }
                        catch (Exception e)
                        {
                            log.Error("Exception occurred: {0}", e.ToString());
                        }

                        if (!success)
                        {
                            log.Warn("Rolling back transaction.");
                            unitOfWork.SafeTransactionRollback(transaction);
                        }

                        unitOfWork.ReleaseLock(lockObjects);
                    }
                }
            }
            else
            {
                log.Debug("No neighbors announced by LOC server.");
                res = true;
            }

            if (signalActionProcessor)
            {
                NeighborhoodActionProcessor neighborhoodActionProcessor = (NeighborhoodActionProcessor)Base.ComponentDictionary[NeighborhoodActionProcessor.ComponentName];
                neighborhoodActionProcessor.Signal();
            }

            if (res && IsInitialization)
            {
                log.Debug("LOC component is now considered in sync with LOC server.");
                serverComponent.SetLocServerInitialized(true);
            }

            log.Trace("(-):{0}", res);
            return(res);
        }