Avoid multiple instances of socket connections

The server socket may be offline for an unkown duration. During that time the app tries to connect every 5 seconds.

But if the server is not reachable for let's say 1 min, multiple connection tries are being requested.

If the server is back online, multiple (not all) connections are realized.

I want to ensure that there is only one request for a connection at a time.

But during the fix 2 min connection timeout a reconnect normally fails.

I assumed that during connection timeout a reconnect is possible:

Pseudo code:

bool connected = false;
bool tryingToConnect = false;
Socket socket;

Timer_every_5_sec {
 if(!connected&&!tryingToConnect) PleaseConnect

PleaseConnect {
        tryingToConnect = true;
        socket = await Socket.connect(IP,Port); //timeout is by default 2 min
        tryingToConnect = false;