Moving drainBuffer() call to execute after socket is closed