Turned out that my first fix (which I also posted here) introduced one tricky possibility for a deadlock somewhere in the JVM shutdown hooks, when the connections are closed due to a shutdown. This sometimes (in maybe 10% of the cases) resulted in JVMs "freezing" during shutdown and not actually exiting the process. They had to be killed forcefully.
I have attached a fixed patch file in which this problem was solved. We haven't had any problems since, neither with freezing JVMs nor with JVMs hitting their heap space limit due to unstable network connections causing hundreds of reconnects.