Added error handling for TLS handshake