Added TLS support.
This is a pretty ugly hack that needs to be cleaned up sooner or later.