Refactor TLS connection