Tls2

Rebased on master, lots of conflicts resolved. The comments at pull #837 still apply.