Every AP has a bssid (MAC address) that you can use to connect to specific AP.
It’s up to the code to figure out which one to connect to. The libraries have all the options.
When you do a scan you get bssid of the AP and strength of each signal. You can make a determination of when to rescan and reconnect.