This commit fixes numerous stability issues with TLS transport code as well as adds unit tests for it.