Thanks for the patches!
I was able to reproduce the issue both on windows, and on linux by
"patching" the server to reproduce the described behavior.
The overall changes look good to me, it just needs some
squashing/organizing to be a proper patchset.