static my_bool mysql_reconnect(MYSQL *mysql)
{
  MYSQL tmp_mysql;
  //--------------PATCH-------------------
  //--Origial free_me flag in mysql object
  my_bool free_me;
  LIST *li_stmt= mysql->stmts;
  DBUG_ENTER("mysql_reconnect");

  if (!mysql->reconnect ||
      (mysql->server_status & SERVER_STATUS_IN_TRANS) || !mysql->host_info)
  {
   /* Allov reconnect next time */
    mysql->server_status&= ~SERVER_STATUS_IN_TRANS;
    my_set_error(mysql, CR_SERVER_GONE_ERROR, SQLSTATE_UNKNOWN, 0);
    DBUG_RETURN(1);
  }

  mysql_init(&tmp_mysql);
  tmp_mysql.options=mysql->options;

  /* don't reread options from configuration files */
  tmp_mysql.options.my_cnf_group= tmp_mysql.options.my_cnf_file= NULL;

  /* make sure that we reconnect with the same character set */
  if (!tmp_mysql.options.charset_name ||
      strcmp(tmp_mysql.options.charset_name, mysql->charset->csname))
  {
    my_free(tmp_mysql.options.charset_name, MYF(MY_ALLOW_ZERO_PTR));
    tmp_mysql.options.charset_name= my_strdup(mysql->charset->csname, MYF(MY_WME));
  }

  tmp_mysql.reconnect= mysql->reconnect;
  //----------PATCH--------------------------
  //--We have to keep the original options---
  //--So don't set the options to zero here--
  //bzero((char*) &mysql->options,sizeof(mysql->options));
  if (!mysql_real_connect(&tmp_mysql,mysql->host,mysql->user,mysql->passwd,
              mysql->db, mysql->port, mysql->unix_socket,
              mysql->client_flag | CLIENT_REMEMBER_OPTIONS))
  {
    my_set_error(mysql, tmp_mysql.net.last_errno, 
                        tmp_mysql.net.sqlstate, 
                        tmp_mysql.net.last_error);
    //--------------PATCH-------------------
    //--If it fails, just destroy the tmp_mysql.
    //--But don't delete the options in tmp_mysql
    //--, because the options are shared between mysql and tmp_mysql
    //--The options need to be reused next time.
    //--Set the options in tmp_mysql to NULL, in order to guard
    //--the original pointers of options in mysql object.
    bzero((char*) &tmp_mysql.options,sizeof(tmp_mysql.options));
    mysql_close(&tmp_mysql);
	DBUG_RETURN(1);
  }

  /* reset the connection in all active statements 
     todo: check stmt->mysql in mysql_stmt* functions ! */
  for (;li_stmt;li_stmt= li_stmt->next)
  {
    MYSQL_STMT *stmt= (MYSQL_STMT *)li_stmt->data;

    if (stmt->state != MYSQL_STMT_INITTED)
    {
      stmt->mysql= NULL;
      stmt->state= MYSQL_STMT_INITTED;
      SET_CLIENT_STMT_ERROR(stmt, CR_SERVER_LOST, SQLSTATE_UNKNOWN, 0);
    }
    else
      tmp_mysql.stmts= list_add(tmp_mysql.stmts, &stmt->list);
  }

  //--------------PATCH-----------------
  //We also have to keep the origial free_me flag
  //The flag needs to be set back after cloning.
  free_me = mysql->free_me;
  mysql->free_me=0;
  mysql->stmts= NULL;
  //--------------PATCH--------------------------
  //--The options should be set to zero before mysql_close()
  //--Otherwise, the options would be deleted in mysql_close()
  memset(&mysql->options, 0, sizeof(mysql->options));
  mysql_close(mysql);
  //memset(&mysql->options, 0, sizeof(mysql->options));
  //---------CLONING a COPY from tmp_mysql---------
  *mysql=tmp_mysql;
  //--Now, set the original free_me flag back.
  mysql->free_me = free_me;
  mysql->reconnect= 1;
  net_clear(&mysql->net);
  mysql->affected_rows= ~(my_ulonglong) 0;
  DBUG_RETURN(0);
}
