TLS connection code rewrite