             return
         }
         let peripherals = self.peripheralsSortedByRSSI
-        if allPeripheralsDiscovered {
-            for _ in 0..<maxConnections {
-                let peripheralIndex = Int(arc4random_uniform(UInt32(peripherals.count)))
-                let peripheral = peripherals[peripheralIndex]
-                Logger.debug("Connecting peripheral: '\(peripheral.name)', \(peripheralIndex), \(peripheral.identifier.uuidString)")
+        for peripheral in peripherals where connectionCount < maxConnections {
+            if !connectedPeripherals.contains(peripheral.identifier) && !discoveredPeripherals.contains(peripheral.identifier) {
+                Logger.debug("Connecting peripheral: '\(peripheral.name)', \(peripheral.identifier.uuidString)")
                 connectedPeripherals.insert(peripheral.identifier)
                 connect(peripheral)
-            }
-        } else {
-            for peripheral in peripherals where connectionCount < maxConnections {
-                if !connectedPeripherals.contains(peripheral.identifier) && !discoveredPeripherals.contains(peripheral.identifier) {
-                    Logger.debug("Connecting peripheral: '\(peripheral.name)', \(peripheral.identifier.uuidString)")
-                    connectedPeripherals.insert(peripheral.identifier)
-                    connect(peripheral)
-                    connectionCount += 1
-                }
+                connectionCount += 1
             }
         }
     }

        let cell = tableView.dequeueReusableCell(withIdentifier: MainStoryboard.peripheralCell, for: indexPath) as! PeripheralCell
        let peripheral = self.peripherals[indexPath.row]
        cell.nameLabel.text = peripheral.name
        if peripheral.state == .connected || discoveredPeripherals.contains(peripheral.identifier) {
            cell.nameLabel.textColor = UIColor.black
        } else {
            cell.nameLabel.textColor = UIColor.lightGray

                    strongSelf.alertAndStopScanning(message: "CentralManager state \"\(state.stringValue)\"")
                case .resetting:
                    strongSelf.alertAndStopScanning(message:
                        "CentralManager state \"\(state.stringValue)\". The connection with the system bluetooth service was momentarily lost. Restart scan.")
                case .unsupported:
                    strongSelf.alertAndStopScanning(message: "CentralManager state \"\(state.stringValue)\". Bluetooth not supported.")
                }
