added ssl_write error check and changed thread capture to reference