Fix a dead lock which occurs when multiple threads attempt to close the same SSL connection

- Fixes #1817