On error, write error to client handle, and to thread local storage

As mentioned in #382 client handle errbuf is freed before the caller gets a chance to look at it.

There is no standard for thread local storage in C, though the major compilers GCC, clang and visual studio all have support.

It is possible to add TLS support by using the pthread functions, but to be honest, it's sort of a PITA and probably not worth it for something so trivial.