Because it client / server, the client always connects to the server.
the client needs a loop. When it tries to connect it may fail. The client should delay and try again. When connected, it may lose the connection, so it should go back to the connect loop.
the server just waits for a connection. If the connection is lost, it goes back to waiting.
both the client and need to keep track of the connection status. If live they can send a message, if not then wait until the connection is restored. Detecting lost connection will typically happen on a message send. App often implements a ping/keep alive message they send to more quickly detect a lost connection .