Fixes #2735 (closed).
This ports !1887 (merged) that solves #2688 (closed) to Kea 2.2.x. This is a purely mechanical port; it only rebases the commits on top of branch v2_2
and fixes merge conflicts.
We encountered the same bug as the original reporter; with this patch in place the bug went away.