Each time when you connect you apparently use client ID zero: eConnect(Host, Port, 0). When you disconnect this client you must allow IBKR time to close all processes related to this client on their side. According to an undocumented recommendation from IBKR do you need to wait approximately one minute before you try to connect again to the same client ID number (zero, in your case). Your post does not mention how long you are waiting after closing before trying to reconnect to that client.
If you can't wait one minute before reconnecting then you should simply use a different client ID number. Any integer number can be used, there are no rules for this. So you could use for example a randomly generated ID, or a timestamp as ID (e.g. hhmmss). It is documented that you can have a maximum of 32 clients connected at the same time.