/**
         * Function that tries to connect to a given IP address
         */
        private async Task IPConnect(string selectedIP, string selectedName)
        {
            //Handle the dummy connection
            if (selectedIP == "DummyIP")
            {
                var dummyConnection = new DummyConnection(selectedName, selectedIP);
                _mainWindow.deviceListMain.Add(dummyConnection);
                _mainWindow.LogField.AppendText(DateTime.Now + ":\t" + "Added " + selectedName + " for testing\n");
                LogFieldReg.AppendText("Added " + selectedName + " for testing\n");
            }

            else if (!CheckIfValidIP(selectedIP))
            {
                LogFieldReg.AppendText("Invalid IP used, try again!\n");
                _mainWindow.LogField.AppendText(DateTime.Now + ":\tInvalid IP used, try again!\n");
            }

            else
            {
                PiCarConnection newConnection = null;
                var             canConnect    = false;
                try
                {
                    newConnection = new PiCarConnection(selectedName, selectedIP);
                    var connectResponse = newConnection.RequestConnect();
                    Console.Write(connectResponse.Item2);
                    LogFieldReg.AppendText(connectResponse.Item2);
                    _mainWindow.LogField.AppendText(DateTime.Now + ":\t" + connectResponse.Item2);
                    canConnect = connectResponse.Item1;
                }
                catch (RpcException rpcE)
                {
                    _mainWindow.LogField.AppendText(DateTime.Now + ":\tError! " + rpcE + "\n");
                }
                catch (Exception exception)
                {
                    _mainWindow.LogField.AppendText(DateTime.Now + ":\tError! " + exception + "\n");
                }

                if (canConnect)
                {
                    _mainWindow.LogField.AppendText(DateTime.Now + ":\t" + "Connected to " + selectedName + " with IP: " + selectedIP + "\n");
                    LogFieldReg.AppendText("Connected to " + selectedName + " with IP: " + selectedIP + "\n");
                    _mainWindow.deviceListMain.Add(newConnection);
                }
                else
                {
                    _mainWindow.LogField.AppendText(DateTime.Now + ":\t" + "Failed to connect to " + selectedName + " with IP: " + selectedIP + "\n");
                    LogFieldReg.AppendText("Failed to connect to " + selectedName + " with IP: " + selectedIP + "\n");
                }
            }
        }
        /**
         *
         */
        private async void BackgroundWorker1_DoWorkAsync(object sender, DoWorkEventArgs e)
        {
            var tasks = new List <Task>(); //sets tasks to be a new List of Tasks

            _devicesFound = 0;             //sets devices found to 0
            Dispatcher.Invoke(() =>        //making changes to the main thread
            {
                LogFieldReg.AppendText("Starting scan\n");
                LogFieldReg.AppendText("Default Gateway: " + _defaultGateway);
                _mainWindow.LogField.AppendText(DateTime.Now + ":\tStarting scan\n");
                buttonScan.IsEnabled   = false;
                CancelButton.IsEnabled = true;
                Scan.IsEnabled         = false;
            });                                           //end of current changes to the main thread
            var stopWatch = new Stopwatch();              //wipes the stopwatch

            stopWatch.Start();                            //starts the stopwatch

            for (var i = firstPicar; i <= lastPicar; i++) //for Picar IP range
            {
                _scanningIp = _defaultGateway + i;
                var p = new Ping();

                var task = AsyncUpdate(p, _scanningIp);     //make a task to ping and identify the current IP
                tasks.Add(task);                            //add it to the task list

                if (_backgroundWorker1.CancellationPending) //if a cancellation request has been sent
                {
                    Dispatcher.Invoke(() =>
                    {
                        tasks.Clear(); //wipe the task list
                        LogFieldReg.AppendText("Scan aborted \n");
                        _mainWindow.LogField.AppendText("Scan aborted\n");
                    });
                    break; //break out of the ip for loop scan
                }
            }
            await Task.WhenAll(tasks); //asyncronously wait for all the tasks to complete

            stopWatch.Stop();          //stop the stopwatch
            _ts = stopWatch.Elapsed;   //assign the time it took to a variable

            Dispatcher.Invoke(() =>
            {
                LogFieldReg.AppendText("Finsished scan (" + _ts.TotalSeconds + " seconds)\nDetected " + _devicesFound + " devices\n");
                _mainWindow.LogField.AppendText(DateTime.Now + ":\tFinsished scan: " + _ts.TotalSeconds + " seconds\n" + DateTime.Now + "\tDetected " + _devicesFound + " devices\n");
                buttonScan.IsEnabled   = true;
                CancelButton.IsEnabled = false;
                Scan.IsEnabled         = true;
            });
        }
        /**
         * Constructor for Registration Window
         * Checks for gateway IP and sets the output.
         */
        public Registration()
        {
            InitializeComponent();
            InitializeBackgroundWorker();


            _backgroundWorker1.WorkerSupportsCancellation = true; //Makes the backgroundworker able to recieve cancellation requests

            CancelButton.IsEnabled = false;
            //allows pressing enter button to try connecting
            var submit = new RoutedCommand();

            submit.InputGestures.Add(new KeyGesture(Key.Enter));
            CommandBindings.Add(new CommandBinding(submit, TryConnect));
            //Finds default gateway IP
            DeviceList.ItemsSource = null;
            DeviceList.ItemsSource = deviceStringList.Select(array => array.FirstOrDefault());
            try
            {
                foreach (var curInterface in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (curInterface.OperationalStatus != OperationalStatus.Up)
                    {
                        continue;
                    }
                    foreach (var gatewayOutput in curInterface.GetIPProperties()
                             .GatewayAddresses)
                    {
                        _defaultGateway = gatewayOutput.Address.ToString();
                    }
                }
            }
            catch (NetworkInformationException exception)
            {
                LogFieldReg.AppendText(DateTime.Now + ":\tDevice not connected to the internet! " + exception);
                this.Close();
            }

            _defaultGateway = _defaultGateway.Substring(0, _defaultGateway.Length - 1);
            LogFieldReg.AppendText("The gateway IP is " + _defaultGateway + "x\n");
            _mainWindow.LogField.AppendText(DateTime.Now + ":\tThe gateway IP is " + _defaultGateway + "x\n");
        }