Index: wp-includes/wp-db.php
===================================================================
--- wp-includes/wp-db.php	(revision 7448)
+++ wp-includes/wp-db.php	(working copy)
@@ -43,6 +43,10 @@
 			'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
 	var $charset;
 	var $collate;
+	var $db_user;
+	var $db_password;
+	var $db_name;
+	var $db_host;
 
 	/**
 	 * Connects to the database server and selects a database
@@ -67,8 +71,21 @@
 		if ( defined('DB_COLLATE') )
 			$this->collate = DB_COLLATE;
 
-		$this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
-		if (!$this->dbh) {
+		$this->db_user = $dbuser;
+		$this->db_password = $dbpassword;
+		$this->db_name = $dbname;
+		$this->db_host = $dbhost;
+
+		$this->connect_to_db();
+	}
+
+	function __destruct() {
+		return true;
+	}
+
+	function connect_to_db() {
+		$this->dbh = @mysql_connect($this->db_host, $this->db_user, $this->db_password, true);
+		if ( !$this->dbh ) {
 			$this->bail("
 <h1>Error establishing a database connection</h1>
 <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
@@ -87,13 +104,33 @@
 		if ( !empty($this->charset) && version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=') )
  			$this->query("SET NAMES '$this->charset'");
 
-		$this->select($dbname);
+		$this->select($this->db_name);
 	}
 
-	function __destruct() {
-		return true;
+	function check_connection() {
+		$maxcount = 2;
+		$count = 0;
+
+		$ping = mysql_ping( $this->dbh ) ;
+
+		while ( !$ping && $count < $maxcount ) {
+			@mysql_close($this->dbh);
+			sleep(2);
+			if ($this->connect_to_db() )
+			{
+				$ping = mysql_ping( $this->dbh ) ;
+
+				if ( $ping )
+					break;
+			}
+			sleep(2);
+			$count++;
+		}
+
+		if ( !$ping )
+			$this->bail('Lost connection to server');
 	}
-
+ 
 	function set_prefix($prefix) {
 
 		if ( preg_match('|[^a-z0-9_]|i', $prefix) )
@@ -280,8 +317,18 @@
 
 		// If there is an error then take note of it..
 		if ( $this->last_error = mysql_error($this->dbh) ) {
-			$this->print_error();
-			return false;
+			$errno = mysql_errno();
+			if ( 2006 == $errno || 2013 == $errno ) {
+				$this->check_connection();
+				$this->result = @mysql_query($query, $this->dbh);
+				if ( $this->last_error = mysql_error($this->dbh) ) {
+					$this->print_error();
+					return false;
+				}
+			} else {
+				$this->print_error();
+				return false;
+			}
 		}
 
 		if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
