/// <summary>
        /// Remove the client.
        /// </summary>
        /// <param name="uniqueIdentifier">The unique client identifier.</param>
        /// <param name="serviceName">The service name the client is connected to.</param>
        /// <param name="activeConnections">The number of active connection using this client.</param>
        /// <param name="context">The communication data.</param>
        /// <returns>The object containing the new data; else null.</returns>
        public static bool RemoveClient(string uniqueIdentifier, string serviceName, int activeConnections,
                                        Nequeo.Xml.Authorisation.Communication.Data.contextService context)
        {
            // Validate.
            if (String.IsNullOrEmpty(uniqueIdentifier))
            {
                throw new ArgumentNullException("uniqueIdentifier");
            }
            if (String.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentNullException("serviceName");
            }

            try
            {
                // Find all clients with unique identifier.
                Communication.Data.contextServiceClient client = null;

                try
                {
                    // Find the first item.
                    client = context.clients.First(
                        u => (u.uniqueIdentifier.ToString().ToLower() == uniqueIdentifier.ToLower()) &&
                        (u.serviceName.ToLower() == serviceName.ToLower()));
                }
                catch { }

                if (client != null)
                {
                    // If there is one or less active client connections
                    // then remove the client.
                    if (activeConnections < 2)
                    {
                        // Find the index of the client to remove.
                        context.clients = context.clients.Remove(u => u.Equals(client));
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Get the host details for the identities.
        /// </summary>
        /// <param name="uniqueIdentifier">The unique client identifier.</param>
        /// <param name="serviceName">The service name the client is connected to.</param>
        /// <param name="context">The communication data.</param>
        /// <returns>The host details containing the name (host); else null.</returns>
        public static string GetHost(string uniqueIdentifier, string serviceName, Nequeo.Xml.Authorisation.Communication.Data.contextService context)
        {
            // Validate.
            if (String.IsNullOrEmpty(uniqueIdentifier))
            {
                throw new ArgumentNullException("uniqueIdentifier");
            }
            if (String.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentNullException("serviceName");
            }

            try
            {
                // Find all clients with unique identifier.
                Communication.Data.contextServiceClient client = null;

                try
                {
                    // Find the first item.
                    client = context.clients.First(
                        u => (u.uniqueIdentifier.ToString().ToLower() == uniqueIdentifier.ToLower()) &&
                        (u.serviceName.ToLower() == serviceName.ToLower()));
                }
                catch { }

                if (client != null)
                {
                    // Get the host.
                    string hostName = client.host;

                    // Return the host information.
                    return(hostName);
                }

                return(null);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Get the communication active state.
        /// </summary>
        /// <param name="uniqueIdentifier">The unique client identifier.</param>
        /// <param name="serviceName">The service name the client is connected to.</param>
        /// <param name="context">The communication data.</param>
        /// <returns>True if active; else false.</returns>
        public static bool GetClientActive(string uniqueIdentifier, string serviceName, Nequeo.Xml.Authorisation.Communication.Data.contextService context)
        {
            // Validate.
            if (String.IsNullOrEmpty(uniqueIdentifier))
            {
                throw new ArgumentNullException("uniqueIdentifier");
            }
            if (String.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentNullException("serviceName");
            }

            try
            {
                // Find all clients with unique identifier.
                Communication.Data.contextServiceClient client = null;

                try
                {
                    // Find the first item.
                    client = context.clients.First(
                        u => (u.uniqueIdentifier.ToString().ToLower() == uniqueIdentifier.ToLower()) &&
                        (u.serviceName.ToLower() == serviceName.ToLower()));
                }
                catch { }

                if (client != null)
                {
                    // Get the client reference.
                    return(client.active);
                }

                return(false);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Add a new client.
        /// </summary>
        /// <param name="uniqueIdentifier">The unique client identifier.</param>
        /// <param name="serviceName">The service name the client is connected to.</param>
        /// <param name="hostName">The host machine the client must connect to.</param>
        /// <param name="context">The communication data.</param>
        /// <param name="active">Is the communication currenlty active.</param>
        /// <param name="communicationToken">The common token used for communication.</param>
        /// <returns>The object containing the new data; else null.</returns>
        public static bool AddClient(string uniqueIdentifier, string serviceName, string hostName,
                                     Nequeo.Xml.Authorisation.Communication.Data.contextService context, bool active = false, string communicationToken = "")
        {
            // Validate.
            if (String.IsNullOrEmpty(uniqueIdentifier))
            {
                throw new ArgumentNullException("uniqueIdentifier");
            }
            if (String.IsNullOrEmpty(hostName))
            {
                throw new ArgumentNullException("hostName");
            }
            if (String.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentNullException("serviceName");
            }

            try
            {
                // Find all clients with unique identifier.
                Communication.Data.contextServiceClient client = null;

                try
                {
                    // Find the first item.
                    client = context.clients.First(
                        u => (u.uniqueIdentifier.ToString().ToLower() == uniqueIdentifier.ToLower()) &&
                        (u.serviceName.ToLower() == serviceName.ToLower()) &&
                        (u.host.ToLower() == hostName.ToLower()));
                }
                catch { }

                if (client != null)
                {
                    // Get the client reference.
                    client.active    = active;
                    client.dateAdded = DateTime.Now;
                    client.Value     = (String.IsNullOrEmpty(communicationToken) ? "" : communicationToken);

                    // Save the new data.
                    SaveClientServiceDataAsync(context);
                    return(true);
                }
                else
                {
                    // Load all the clients into a temp list.
                    List <Communication.Data.contextServiceClient> tempClients = new List <Communication.Data.contextServiceClient>(context.clients);
                    Communication.Data.contextServiceClient        clientData  = new Communication.Data.contextServiceClient()
                    {
                        uniqueIdentifier = Int32.Parse(uniqueIdentifier),
                        host             = hostName,
                        serviceName      = serviceName,
                        active           = active,
                        dateAdded        = DateTime.Now,
                        Value            = (String.IsNullOrEmpty(communicationToken) ? "" : communicationToken)
                    };

                    // Add the client from the list.
                    tempClients.Add(clientData);

                    // Assign the new client list to the
                    // new context data.
                    context.clients = tempClients.ToArray();
                    return(true);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }