diff --git a/src/connectionmanager.cpp b/src/connectionmanager.cpp
index 339c8d56ec5b6b76f753ce8320fa3ed3bb945c4f..67623b2f1bb526556be127cda9ebd6014ac044a8 100644
--- a/src/connectionmanager.cpp
+++ b/src/connectionmanager.cpp
@@ -747,15 +747,16 @@ ConnectionManager::Impl::connectDevice(const std::shared_ptr<dht::crypto::Certif
             cb(nullptr, deviceId);
             return;
         }
-        dht::Value::Id vid = ValueIdDist(1, ID_MAX_VAL)(sthis->rand);
+        dht::Value::Id vid;
         auto isConnectingToDevice = false;
         {
             std::lock_guard<std::mutex> lk(sthis->connectCbsMtx_);
+            vid = ValueIdDist(1, ID_MAX_VAL)(sthis->rand);
             auto pendingsIt = sthis->pendingOperations_.find(deviceId);
             if (pendingsIt != sthis->pendingOperations_.end()) {
                 const auto& pendings = pendingsIt->second;
                 while (pendings.connecting.find(vid) != pendings.connecting.end()
-                       && pendings.waiting.find(vid) != pendings.waiting.end()) {
+                    || pendings.waiting.find(vid) != pendings.waiting.end()) {
                     vid = ValueIdDist(1, ID_MAX_VAL)(sthis->rand);
                 }
             }