/// <summary>
        /// Processes update received from LOC server that informs the proximity server about a new neighbor server or a change in existing neighbor server contact information.
        /// </summary>
        /// <param name="UnitOfWork">Unit of work instance.</param>
        /// <param name="ServerId">Network identifier of the neighbor server.</param>
        /// <param name="IpAddress">IP address of the neighbor server.</param>
        /// <param name="Port">Primary interface port of the neighbor server.</param>
        /// <param name="Latitude">GPS location latitude of the neighbor server.</param>
        /// <param name="Longitude">GPS location longitude of the neighbor server.</param>
        /// <param name="NeighborhoodSize">Size of the proximity server's neighborhood at the moment the function is called.</param>
        /// <returns>Information related to how should the caller proceed further, described in AddOrChangeNeighborResult structure.</returns>
        /// <remarks>The caller is responsible for calling this function within a database transaction with NeighborLock and NeighborhoodActionLock locks.</remarks>
        public async Task <AddOrChangeNeighborResult> AddOrChangeNeighborAsync(UnitOfWork UnitOfWork, byte[] ServerId, IPAddress IpAddress, int Port, int Latitude, int Longitude, int NeighborhoodSize)
        {
            log.Trace("(ServerId:'{0}',IpAddress:{1},Port:{2},Latitude:{3},Longitude:{4},NeighborhoodSize:{5})", ServerId.ToHex(), IpAddress, Port, Latitude, Longitude, NeighborhoodSize);

            AddOrChangeNeighborResult res = new AddOrChangeNeighborResult();

            res.NeighborhoodSize = NeighborhoodSize;

            // Data validation.
            bool serverIdValid = ServerId.Length == ProtocolHelper.NetworkIdentifierLength;

            if (!serverIdValid)
            {
                log.Error("Received invalid neighbor server ID '{0}' from LOC server.", ServerId.ToHex());
                res.Error = true;
                log.Trace("(-):*.Error={0},*.SaveDb={1},*.SignalActionProcessor={2},*.NeighborhoodSize={3}", res.Error, res.SaveDb, res.SignalActionProcessor, res.NeighborhoodSize);
                return(res);
            }

            bool portValid = (0 < Port) && (Port <= 65535);

            if (!portValid)
            {
                log.Error("Received invalid neighbor server port '{0}' from LOC server.", Port);
                res.Error = true;
                log.Trace("(-):*.Error={0},*.SaveDb={1},*.SignalActionProcessor={2},*.NeighborhoodSize={3}", res.Error, res.SaveDb, res.SignalActionProcessor, res.NeighborhoodSize);
                return(res);
            }

            IopProtocol.GpsLocation location = new IopProtocol.GpsLocation(Latitude, Longitude);
            if (!location.IsValid())
            {
                log.Error("Received invalid neighbor server location '{0}' from LOC server.", location);
                res.Error = true;
                log.Trace("(-):*.Error={0},*.SaveDb={1},*.SignalActionProcessor={2},*.NeighborhoodSize={3}", res.Error, res.SaveDb, res.SignalActionProcessor, res.NeighborhoodSize);
                return(res);
            }

            // Data processing.
            Neighbor existingNeighbor = (await UnitOfWork.NeighborRepository.GetAsync(n => n.NetworkId == ServerId)).FirstOrDefault();

            if (existingNeighbor == null)
            {
                // New neighbor server.
                if (NeighborhoodSize < Config.Configuration.MaxNeighborhoodSize)
                {
                    // We have not reached the maximal size of the neighborhood yet, the server can be added.
                    log.Trace("New neighbor ID '{0}' detected, IP address {1}, port {2}, latitude {3}, longitude {4}.", ServerId.ToHex(), IpAddress, Port, Latitude, Longitude);

                    // Add neighbor to the database of neighbors.
                    // The neighbor is not initialized, so we will not allow it to send us
                    // any updates. First, we need to contact it and start the neighborhood initialization process.
                    Neighbor neighbor = new Neighbor()
                    {
                        NetworkId         = ServerId,
                        IpAddress         = IpAddress.GetAddressBytes(),
                        PrimaryPort       = Port,
                        NeighborPort      = null,
                        LocationLatitude  = location.Latitude,
                        LocationLongitude = location.Longitude,
                        LastRefreshTime   = DateTime.UtcNow,
                        Initialized       = false,
                        SharedActivities  = 0
                    };
                    await UnitOfWork.NeighborRepository.InsertAsync(neighbor);

                    res.NeighborhoodSize++;

                    // This action will cause our proximity server to contact the new neighbor server and ask it to share its activity database,
                    // i.e. the neighborhood initialization process will be started.
                    // We set a delay depending on the number of neighbors, so that a new server joining a neighborhood is not overwhelmed with requests.
                    int delay = RandomSource.Generator.Next(0, 3 * res.NeighborhoodSize);

                    NeighborhoodAction action = new NeighborhoodAction()
                    {
                        ServerId              = ServerId,
                        Timestamp             = DateTime.UtcNow,
                        Type                  = NeighborhoodActionType.AddNeighbor,
                        ExecuteAfter          = DateTime.UtcNow.AddSeconds(delay),
                        TargetActivityId      = 0,
                        TargetActivityOwnerId = null,
                        AdditionalData        = null,
                    };
                    await UnitOfWork.NeighborhoodActionRepository.InsertAsync(action);

                    res.SignalActionProcessor = true;
                    res.SaveDb = true;
                }
                else
                {
                    log.Error("Unable to add new neighbor ID '{0}', the proximity server reached its neighborhood size limit {1}.", ServerId.ToHex(), Config.Configuration.MaxNeighborhoodSize);
                }
            }
            else
            {
                // This is a neighbor we already know about. Just check that its information is up to date and if not, update it.
                IPAddress existingNeighborIpAddress = new IPAddress(existingNeighbor.IpAddress);
                if (!existingNeighborIpAddress.Equals(IpAddress))
                {
                    log.Trace("Existing neighbor ID '{0}' changed its IP address from {1} to {2}.", ServerId.ToHex(), existingNeighborIpAddress, IpAddress);
                    existingNeighbor.IpAddress = IpAddress.GetAddressBytes();
                }

                if (existingNeighbor.PrimaryPort != Port)
                {
                    // Primary port was change, so we also expect that the neighbors interface port was changed as well.
                    log.Trace("Existing neighbor ID '{0}' changed its primary port from {1} to {2}, invalidating neighbors interface port as well.", ServerId.ToHex(), existingNeighbor.PrimaryPort, Port);
                    existingNeighbor.PrimaryPort  = Port;
                    existingNeighbor.NeighborPort = null;
                }

                if (existingNeighbor.LocationLatitude != location.Latitude)
                {
                    log.Trace("Existing neighbor ID '{0}' changed its latitude from {1} to {2}.", ServerId.ToHex(), existingNeighbor.LocationLatitude, location.Latitude);
                    existingNeighbor.LocationLatitude = Latitude;
                }

                if (existingNeighbor.LocationLongitude != location.Longitude)
                {
                    log.Trace("Existing neighbor ID '{0}' changed its longitude from {1} to {2}.", ServerId.ToHex(), existingNeighbor.LocationLongitude, location.Longitude);
                    existingNeighbor.LocationLongitude = Longitude;
                }

                // We consider a fresh LOC info to be accurate, so we do not want to delete the neighbors received here
                // and hence we update their refresh time.
                existingNeighbor.LastRefreshTime = DateTime.UtcNow;

                UnitOfWork.NeighborRepository.Update(existingNeighbor);
                res.SaveDb = true;
            }

            log.Trace("(-):*.Error={0},*.SaveDb={1},*.SignalActionProcessor={2},*.NeighborhoodSize={3}", res.Error, res.SaveDb, res.SignalActionProcessor, res.NeighborhoodSize);
            return(res);
        }
        /// <summary>
        /// Processes NeighbourhoodChangedNotificationRequest message from LOC server.
        /// <para>Adds, changes, or deletes neighbor and possibly adds new neighborhood action to the database.</para>
        /// </summary>
        /// <param name="Client">TCP client who received the message.</param>
        /// <param name="RequestMessage">Full request message.</param>
        /// <returns>Response message to be sent to the client.</returns>
        public async Task <LocProtocolMessage> ProcessMessageNeighbourhoodChangedNotificationRequestAsync(LocClient Client, LocProtocolMessage RequestMessage)
        {
            log.Trace("()");

            LocProtocolMessage res     = Client.MessageBuilder.CreateErrorInternalResponse(RequestMessage);
            bool signalActionProcessor = false;

            NeighbourhoodChangedNotificationRequest neighbourhoodChangedNotificationRequest = RequestMessage.Request.LocalService.NeighbourhoodChanged;

            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 (NeighbourhoodChange change in neighbourhoodChangedNotificationRequest.Changes)
                        {
                            // We do ignore errors here for each individual change and just continue processing the next item from the list.
                            log.Trace("Neighborhood change type is {0}.", change.ChangeTypeCase);
                            switch (change.ChangeTypeCase)
                            {
                            case NeighbourhoodChange.ChangeTypeOneofCase.AddedNodeInfo:
                            case NeighbourhoodChange.ChangeTypeOneofCase.UpdatedNodeInfo:
                            {
                                bool     isAdd    = change.ChangeTypeCase == NeighbourhoodChange.ChangeTypeOneofCase.AddedNodeInfo;
                                NodeInfo nodeInfo = isAdd ? change.AddedNodeInfo : change.UpdatedNodeInfo;

                                // 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))
                                {
                                    break;
                                }

                                NodeContact            contact   = nodeInfo.Contact;
                                IPAddress              ipAddress = new IPAddress(contact.IpAddress.ToByteArray());
                                Iop.Locnet.GpsLocation location  = nodeInfo.Location;
                                int latitude  = location.Latitude;
                                int longitude = 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;
                                }

                                break;
                            }

                            case NeighbourhoodChange.ChangeTypeOneofCase.RemovedNodeId:
                            {
                                byte[] serverId = change.RemovedNodeId.ToByteArray();

                                bool serverIdValid = serverId.Length == ProtocolHelper.NetworkIdentifierLength;
                                if (!serverIdValid)
                                {
                                    log.Error("Received invalid neighbor server ID '{0}' from LOC server.", serverId.ToHex());
                                    break;
                                }

                                // Data processing.
                                Neighbor existingNeighbor = (await unitOfWork.NeighborRepository.GetAsync(n => n.NetworkId == serverId)).FirstOrDefault();
                                if (existingNeighbor != null)
                                {
                                    log.Trace("Creating neighborhood action to deleting neighbor ID '{0}' from the database.", serverId.ToHex());

                                    string neighborInfo = JsonConvert.SerializeObject(existingNeighbor);

                                    // Delete neighbor completely.
                                    // This will cause our proximity server to erase all activities of the neighbor that has been removed.
                                    bool deleted = await unitOfWork.NeighborRepository.DeleteNeighborAsync(serverId, -1, true);

                                    if (deleted)
                                    {
                                        // Add action that will contact the neighbor and ask it to stop sending updates.
                                        // Note that the neighbor information will be deleted by the time this action
                                        // is executed and this is why we have to fill in AdditionalData.
                                        NeighborhoodAction stopUpdatesAction = new NeighborhoodAction()
                                        {
                                            ServerId              = serverId,
                                            Timestamp             = DateTime.UtcNow,
                                            Type                  = NeighborhoodActionType.StopNeighborhoodUpdates,
                                            TargetActivityId      = 0,
                                            TargetActivityOwnerId = null,
                                            ExecuteAfter          = DateTime.UtcNow,
                                            AdditionalData        = neighborInfo
                                        };
                                        await unitOfWork.NeighborhoodActionRepository.InsertAsync(stopUpdatesAction);

                                        signalActionProcessor = true;
                                        saveDb = true;
                                    }
                                    else
                                    {
                                        log.Error("Failed to remove neighbor ID '{0}' from the database.", serverId.ToHex());
                                        // This is actually bad, we failed to remove a record from the database, which should never happen.
                                        // We try to insert action to remove this neighbor later, but adding the action might fail as well.
                                        NeighborhoodAction action = new NeighborhoodAction()
                                        {
                                            ServerId              = serverId,
                                            Timestamp             = DateTime.UtcNow,
                                            Type                  = NeighborhoodActionType.RemoveNeighbor,
                                            TargetActivityId      = 0,
                                            TargetActivityOwnerId = null,
                                            AdditionalData        = null
                                        };
                                        await unitOfWork.NeighborhoodActionRepository.InsertAsync(action);

                                        signalActionProcessor = true;
                                        saveDb = true;
                                    }
                                }
                                else
                                {
                                    log.Debug("Neighbor ID '{0}' not found, can not be removed.", serverId.ToHex());
                                    // It can be the case that this node has not an associated proximity server, so in that case we should ignore it.
                                    // If the node has an associated proximity server, then nothing bad really happens here if we have activities
                                    // of such a neighbor in NeighborActivity table. Those entries will expire and will be deleted.
                                }
                                break;
                            }

                            default:
                                log.Error("Invalid neighborhood change type '{0}'.", change.ChangeTypeCase);
                                break;
                            }
                        }

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

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

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

                    unitOfWork.ReleaseLock(lockObjects);
                }
            }

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

            log.Trace("(-):*.Response.Status={0}", res.Response.Status);
            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);
        }