I have the same issue on Windows 9 server, My best guess is an edge case socket error introduced in a fairly recent version of the Gateway software since I started getting this issue late last year after I updated my Gateway, even with my old code that was previously connecting and disconnecting flawlessly for months. I also tried disconnect() to no avail.
I found in an old thread here (I can't seem to find it again now) that the solution is to untick 'Allow connections for localhost only' and then add 127.0.0.1 to Trusted IPs - I didn't like this solution so I just restart the server as needed since that's sufficient for my use case.