/// <summary>
        /// Called when the connection is lost.
        /// </summary>
        /// <param name="error">The socket error that ocurred</param>
        /// <param name="exception">The exception that ocurred.</param>
        private void DisconnectedHandler(SocketError error, Exception exception)
        {
            serverGroup.DisconnectedHandler(connection, this, exception);

            EventHandler <ServerDisconnectedEventArgs> handler = ServerDisconnected;

            if (handler != null)
            {
                void DoServerDisconnectedEvent()
                {
                    long startTimestamp = Stopwatch.GetTimestamp();

                    try
                    {
                        handler?.Invoke(this, new ServerDisconnectedEventArgs(this, error, exception));
                    }
                    catch (Exception e)
                    {
                        serverDisconnectedEventFailuresCounter.Increment();

                        logger.Error("A plugin encountered an error whilst handling the ServerDisconnected event. (See logs for exception)", e);
                    }

                    double time = (double)(Stopwatch.GetTimestamp() - startTimestamp) / Stopwatch.Frequency;

                    serverDisconnectedEventTimeHistogram.Report(time);
                }

                threadHelper.DispatchIfNeeded(DoServerDisconnectedEvent);
            }
        }