/// <summary>
        /// Proivdes a threadsafe way to close the serial ports
        /// </summary>
        /// <param name="index">
        /// Index for Dynamic Server Allocation
        /// </param>
        public static void CloseSerialPortUnexpectedly(int index, Dispatcher uiDispatcher)
        {
            if (!SerialPorts[index].IsOpen)
            {
                return;
            }
            while (SerialPorts[index].IsOpen)
            {
                SerialPorts[index].Close();
            }

            uiDispatcher.Invoke((MethodInvoker) delegate
            {
                ;
                var i = 0;
                i    += AvailableSerialPorts.TakeWhile(e => Guid.Parse(e.UidGuid) == SerialCommunications[index].UidGuid)
                        .Count();

                if (i > (AvailableSerialPorts.Count - 1))
                {
                    i  = 0;
                    i += AvailableModbusSerialPorts
                         .TakeWhile(e => Guid.Parse(e.UidGuid) == SerialCommunications[index].UidGuid)
                         .Count();
                    if (i > (AvailableModbusSerialPorts.Count - 1))
                    {
                        throw new NullReferenceException();
                    }

                    var checklistItem = new CheckListItem
                    {
                        IsChecked = false,
                        Content   = AvailableModbusSerialPorts[i].Content,
                        ItemName  = AvailableModbusSerialPorts[i].ItemName,
                        UidGuid   = AvailableModbusSerialPorts[i].UidGuid
                    };

                    AvailableModbusSerialPorts.RemoveAt(i);
                    AvailableModbusSerialPorts.Insert(i, checklistItem);
                }
                else
                {
                    var checkListItem = new CheckListItem
                    {
                        Content   = AvailableSerialPorts[i].Content,
                        IsChecked = false,
                        ItemName  = AvailableSerialPorts[i].ItemName,
                        UidGuid   = AvailableSerialPorts[i].UidGuid
                    };

                    AvailableSerialPorts.RemoveAt(i);
                    AvailableSerialPorts.Insert(i, checkListItem);
                }

                MessageBox.Show(
                    @"Error with Serial Port. Closing connection. Please check settings and connection and try again.",
                    @"Serial Port Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            });
            SerialPorts          = RemoveAtIndex <SerialPort>(index, SerialPorts);
            SerialCommunications = RemoveAtIndex <SerialCommunication>(index, SerialCommunications);
            StatsGroupings       = RemoveAtIndex <int>(index, StatsGroupings);
            ServerInformation    = RemoveAtIndex <RabbitServerInformation>(index, ServerInformation);
            Lineseries           = RemoveAtIndex <LineSeries>(index, Lineseries);

            while (FactoryChannel[index].IsOpen)
            {
                FactoryChannel[index].Close();
            }
            while (FactoryConnection[index].IsOpen)
            {
                FactoryConnection[index].Close();
            }

            FactoryChannel    = RemoveAtIndex <IModel>(index, FactoryChannel);
            FactoryConnection = RemoveAtIndex <IConnection>(index, FactoryConnection);
            Factory           = RemoveAtIndex <IConnectionFactory>(index, Factory);
        }