Added advanced tls listen function.
Removed duplicated code.