Cleanup OpenSSL locking functions.
Error checking mutexes for OpenSSL.
Introduced proper checking and logging of our locking primitives.

ThreadLock(), ThreadUnlock() should now be preferred against
native pthread_mutex_lock().
Use error checking mutexes for our dbm API.
Assert we're not leaking memory

As suggested by @kacfengine.
Log errors properly for failing pthread operations

As suggested by @kacfengine.
Merge pull request #1611 from jimis/agent_protocol_version

Error checking mutexes and detection of errors.