ESDEV-2145 Fix bug #5809: Session ID Disclosure

Ported the fix from 5.2 very roughtly.
(cherry picked from commit fab91c0)