Fix run_query connection timeout issues.

Solves run_query timeout issues by creating a new connection if the last one times out.
Split this up into two parts, the querying process (db_query) and the error checking bit (run_query)