Trying to make a "Direct Code" lobby connect system

Greetings,

You might not be clear with what I am meaning in the title. So I want to make a lobby system where people can connect with a code. This action is done between places (preferably). So the host that makes the lobby is given a code that can be used by other people to join that lobby. The code will be random (for example: “8506”) and it should check if the code already exists and is being used by someone else’s lobby. The lobby owner is being sent to a different place (not game) where the lobby is hosted. Now I am wondering how to make that code so it can be used by the lobby and player that tries to connect to that specific lobby. Any ideas?