Use mutex for connection map.

Move caster logic to caster functions and make struct members private.