* Do not spin if TCP connection blocks for writing (and add test case).
* Fail queries if TCP dies repeatedly, rather than retrying many times.
* Do not abort in a couple of places if TCP unexpectedly broken.
+ * Do not free something twice if query fails and is then cancelled.
Portability/compilation fixes:
* Move `extern "C" {' to after #include <...>'s.