After a big valgrind memory check, the following changes were made .

1) All memory leaks when running the main daemon are fixed.

2) Made all threaded functions platform independent.

3) Nearly all threads will now gracefully close instead of
being forced to. To allow this, some changes were made:
- The socket select function needed to be woken up so it
stopped blocking the execution of a thread. To allow
this, several loopback connection were made to send
a single bit to the socket connection when a specific library
is garbage collecting.
- All mutexes are unlocked and thread conditions are signaled
to continue execution.

4) Some thread helper functions have been added to the protocol
library. This makes protocol writing easier. It also
allows threads to gracefully stop like described in point 3.

5) A special garbage collector function was added to the protocol
library. When the protocols are freed the protocol specific "gc"
function is called first. This allows protocols to free their local
resources first. This function will only be called when it's
initialized.