diff --git libmariadb/mariadb_lib.c libmariadb/mariadb_lib.c
index 8c2a99b..cf6114a 100644
--- libmariadb/mariadb_lib.c
+++ libmariadb/mariadb_lib.c
@@ -1643,9 +1643,16 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
 
   mysql->client_flag= client_flag;
 
+   {
+       volatile my_bool net_blocking = ma_pvio_is_blocking(net->pvio);
+       if (!net_blocking)
+           ma_pvio_blocking(net->pvio, TRUE, 0);
   if (run_plugin_auth(mysql, scramble_data, scramble_len,
                              scramble_plugin, db))
     goto error;
+       if (!net_blocking)
+           ma_pvio_blocking(net->pvio, FALSE, 0);
+   }
 
   if (mysql->client_flag & CLIENT_COMPRESS)
     net->compress= 1;
@@ -1696,6 +1703,15 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
   return(mysql);
 
 error:
+  // fix for proxysql bug #2623
+  if (mysql->net.extension) {
+     free(mysql->net.extension);
+     mysql->net.extension=NULL;
+  }
+  if (mysql->extension) {
+     free(mysql->extension);
+     mysql->extension=NULL;
+  }
   /* Free alloced memory */
   end_server(mysql);
   /* only free the allocated memory, user needs to call mysql_close */
@@ -1775,7 +1791,7 @@ my_bool STDCALL mariadb_reconnect(MYSQL *mysql)
   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) ||
-      mysql_set_character_set(&tmp_mysql, mysql->charset->csname))
+      mysql_set_character_set(&tmp_mysql, mysql->charset->csname, 0))
   {
     if (ctxt)
       my_context_install_suspend_resume_hook(ctxt, NULL, NULL);
@@ -1821,6 +1837,7 @@ void ma_invalidate_stmts(MYSQL *mysql, const char *function_name)
 {
   if (mysql->stmts)
   {
+/*
     LIST *li_stmt= mysql->stmts;
 
     for (; li_stmt; li_stmt= li_stmt->next)
@@ -1829,6 +1846,7 @@ void ma_invalidate_stmts(MYSQL *mysql, const char *function_name)
       stmt->mysql= NULL;
       SET_CLIENT_STMT_ERROR(stmt, CR_STMT_CLOSED, SQLSTATE_UNKNOWN, function_name);
     }
+*/
     mysql->stmts= NULL;
   }
 }
@@ -2115,6 +2133,44 @@ mysql_close(MYSQL *mysql)
   return;
 }
 
+void STDCALL
+mysql_close_no_command(MYSQL *mysql)
+{
+  if (mysql)                    /* Some simple safety */
+  {
+
+    if (mysql->methods) {
+            if (mysql->net.pvio) {
+                free_old_query(mysql);
+                mysql->status=MYSQL_STATUS_READY; /* Force command */
+            mysql->options.reconnect=0;
+                end_server(mysql);
+            }
+        }
+    mysql_close_memory(mysql);
+    mysql_close_options(mysql);
+    mysql->host_info=mysql->user=mysql->passwd=mysql->db=0;
+
+    // fix for proxysql bug #3204
+    if (mysql->net.extension) {
+       free(mysql->net.extension);
+       mysql->net.extension=NULL;
+    }
+
+    if (mysql->extension) {
+       free(mysql->extension);
+       mysql->extension=NULL;
+    }
+
+    /* Clear pointers for better safety */
+    bzero((char*) &mysql->options,sizeof(mysql->options));
+    mysql->net.pvio= 0;
+    if (mysql->free_me)
+      free(mysql);
+  }
+  return;
+}
+
 
 /**************************************************************************
 ** Do a query. If query returned rows, free old rows.
@@ -2189,6 +2245,8 @@ int ma_read_ok_packet(MYSQL *mysql, uchar *pos, ulong length)
             char *data;
             si_type= (enum enum_session_state_type)net_field_length(&pos);
             switch(si_type) {
+            case SESSION_TRACK_GTIDS:
+              net_field_length(&pos); /* skip encoding */
             case SESSION_TRACK_SCHEMA:
             case SESSION_TRACK_STATE_CHANGE:
             case SESSION_TRACK_TRANSACTION_CHARACTERISTICS:
@@ -3670,18 +3728,27 @@ void STDCALL mysql_get_character_set_info(MYSQL *mysql, MY_CHARSET_INFO *cs)
   mariadb_get_charset_info(mysql, cs);
 }
 
-int STDCALL mysql_set_character_set(MYSQL *mysql, const char *csname)
+int STDCALL mysql_set_character_set(MYSQL *mysql, const char *csname, uint charsetnr)
 {
   const MARIADB_CHARSET_INFO *cs;
 
-  if (!csname)
+  if (!csname && !charsetnr)
     goto error;
 
-  if ((cs= mysql_find_charset_name(csname)))
+  if (csname) {
+    cs = mysql_find_charset_name(csname);
+  } else {
+    cs = mysql_find_charset_nr(charsetnr); 
+  }
+  if (cs)
   {
-    char buff[64];
+    char buff[128];
+    if (csname) { // default behavior
+      snprintf(buff, 127, "SET NAMES %s", cs->csname);
+    } else {
+      snprintf(buff, 127, "SET NAMES %s COLLATE '%s'", cs->csname, cs->name);
+    }
 
-    snprintf(buff, 63, "SET NAMES %s", cs->csname);
     if (!mysql_real_query(mysql, buff, (unsigned long)strlen(buff)))
     {
       mysql->charset= cs;
@@ -3691,6 +3758,7 @@ int STDCALL mysql_set_character_set(MYSQL *mysql, const char *csname)
   }
 
 error:
+  if (!cs)
   my_set_error(mysql, CR_CANT_READ_CHARSET, SQLSTATE_UNKNOWN,
                0, csname, "compiled_in");
   return(mysql->net.last_errno);
