fix a bug during in-loop reconnect that references a null pointer, causing segfault.
add sanity checking routine
reorganize, and incorporate a few new functions
clean up some commenting, make some checks debug-only, add new 'wait forever' record retrieval.