Hm, maybe the problem is that your rPI has a short interruption in the WIFI connection, the MQTT TCP connection is interrupted and does not autostart again. Maybe there is something in the logs. Maybe you can find something about reconnects in one of these:
journalctl --unit=systemd-networkd
journalctl --unit=wpa_supplicant
You could try to run wpa_cli in daemon mode to react to disconnects and connects. Create a script like this:
#!/bin/bash
case "$2" in
CONNECTED)
<your command to start the gateway>;
;;
DISCONNECTED)
<your command to stop the gateway>;
;;
esac
then, start wpa_cli in deamon mode:
wpa_cli -a /path/to/your/script #use sudo if your gateway commands need sudo
If that helps, you could create a service from this command.
Regards, Edi