Session locking

Hi

I cherry-picked the commits @mevdschee added to implement session locking, and added the test you requested in his initial pull-request (#109)

Hope you will merge this one ;-)

Regards,