Please find attached a rebased version. I have split the patch into two parts trying to make it easier to review, one with the code changes and the other with the test.
Other than that, there are minimal changes from the previous version to the code due to the update of _WIN32_WINNT and enabling the test on cirrus.