diff --git a/lib/ssh.c b/lib/ssh.c
index 9b37ad6..8dfa09f 100644
--- a/lib/ssh.c
+++ b/lib/ssh.c
@@ -115,6 +115,11 @@ static LIBSSH2_FREE_FUNC(my_libssh2_free);
 
 static CURLcode get_pathname(const char **cpp, char **path);
 
+static CURLcode ssh_set_code_and_ssh_error(struct connectdata *conn, CURLcode code, int ssh_error);
+static CURLcode ssh_set_code_from_ssh_error_with_default_code(struct connectdata *conn, int ssh_error, CURLcode default_code);
+static CURLcode ssh_set_code_from_ssh_error(struct connectdata *conn, int ssh_error);
+static void ssh_quote_fail(struct connectdata *conn, const char* format);
+
 static CURLcode ssh_connect(struct connectdata *conn, bool *done);
 static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done);
 static CURLcode ssh_do(struct connectdata *conn, bool *done);
@@ -593,7 +598,7 @@ static CURLcode ssh_knownhost(struct connectdata *conn)
       state(conn, SSH_SESSION_FREE);
     case CURLKHSTAT_DEFER:
       /* DEFER means bail out but keep the SSH_HOSTKEY state */
-      result = sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
+      result = ssh_set_code_and_ssh_error(conn, CURLE_PEER_FAILED_VERIFICATION, LIBSSH2_ERROR_NONE);
       break;
     case CURLKHSTAT_FINE:
     case CURLKHSTAT_FINE_ADD_TO_FILE:
@@ -663,8 +668,8 @@ static CURLcode ssh_check_fingerprint(struct connectdata *conn)
         failf(data,
             "Denied establishing ssh session: md5 fingerprint not available");
       state(conn, SSH_SESSION_FREE);
-      sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
-      return sshc->actualcode;
+      return ssh_set_code_and_ssh_error(conn, CURLE_PEER_FAILED_VERIFICATION, LIBSSH2_ERROR_NONE);
+
     }
     else {
       infof(data, "MD5 checksum match!\n");
@@ -677,6 +682,105 @@ static CURLcode ssh_check_fingerprint(struct connectdata *conn)
 }
 
 /*
+ * set the actualcode value explicitly
+ * the ssh_error value is returned to in httpcode and can be extracted with CURLINFO_RESPONSE_CODE 
+ */
+
+static CURLcode ssh_set_code_and_ssh_error(struct connectdata *conn,
+                                           CURLcode code,
+                                           int ssh_error)
+{
+    conn->proto.sshc.actualcode = code;
+    conn->data->info.httpcode = ssh_error;
+
+    return code;
+}
+
+/*
+ * set the actualcode value from an SSH error value
+ * if the converted SSH error is zero, we use the supplied default_code value instead for actualcode 
+ */
+
+static CURLcode ssh_set_code_from_ssh_error_with_default_code(struct connectdata *conn,
+                                                              int ssh_error,
+                                                              CURLcode default_code)
+{
+    CURLcode code = libssh2_session_error_to_CURLE(ssh_error);
+    int result = ssh_set_code_and_ssh_error(conn, code ? code : default_code, ssh_error);
+    DEBUGF(infof(conn->data, "error = %d makes libcurl = %d\n",
+                 ssh_error, (int)result));
+
+}
+
+/* 
+ * set the actualcode value from an SSH error value 
+ */
+
+static CURLcode ssh_set_code_from_ssh_error(struct connectdata *conn, int ssh_error)
+{
+    return ssh_set_code_from_ssh_error_with_default_code(conn, ssh_error, CURLE_OK);
+}
+
+
+/* 
+ * fail, reporting the curl error, the reason, and the ssh error if there was one 
+ */
+
+static void ssh_fail_with_result_error_detail(struct connectdata *conn,
+                                              CURLcode result,
+                                              int ssh_error,
+                                              const char* reason,
+                                              const char* detail)
+{
+    struct ssh_conn *sshc = &conn->proto.sshc;
+    if(result == CURLE_OUT_OF_MEMORY)
+        failf(conn->data, "Out of memory");
+    else
+        failf(conn->data, reason, detail);
+    Curl_safefree(sshc->quote_path1);
+    Curl_safefree(sshc->quote_path2);
+    state(conn, SSH_SFTP_CLOSE);
+    sshc->nextstate = SSH_NO_STATE;
+    ssh_set_code_and_ssh_error(conn, result, ssh_error);
+}
+
+/* 
+ * fail, reporting the curl error, and the reason - in this case there is no SSH error to report 
+ */
+
+static void ssh_fail_with_result(struct connectdata *conn,
+                                 CURLcode result,
+                                 const char* reason)
+{
+    ssh_fail_with_result_error_detail(conn, result, LIBSSH2_ERROR_NONE, reason, NULL);
+}
+
+/*
+ * fail, reporting a CURLE_QUOTE_ERROR, and the last ssh error 
+ */
+
+static void ssh_quote_fail(struct connectdata *conn, const char* reason)
+{
+    struct ssh_conn *sshc = &conn->proto.sshc;
+    struct SessionHandle *data = conn->data;
+    int err = sftp_libssh2_last_error(sshc->sftp_session);
+    ssh_fail_with_result_error_detail(conn, CURLE_QUOTE_ERROR, err, reason, sftp_libssh2_strerror(err));
+}
+
+/*
+ * fail, reporting a CURLE_QUOTE_ERROR, with no SSH error
+ */
+
+static void ssh_quote_fail_no_error(struct connectdata *conn, const char* reason)
+{
+    ssh_fail_with_result_error_detail(conn,
+                                      CURLE_QUOTE_ERROR,
+                                      LIBSSH2_ERROR_NONE,
+                                      reason,
+                                      NULL);
+}
+
+/*
  * ssh_statemach_act() runs the SSH state machine as far as it can without
  * blocking and without reaching the end.  The data the pointer 'block' points
  * to will be set to TRUE if the libssh2 function returns LIBSSH2_ERROR_EAGAIN
@@ -702,7 +806,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
     case SSH_INIT:
       sshc->secondCreateDirs = 0;
       sshc->nextstate = SSH_NO_STATE;
-      sshc->actualcode = CURLE_OK;
+      ssh_set_code_and_ssh_error(conn, CURLE_OK, LIBSSH2_ERROR_NONE);
 
       /* Set libssh2 to non-blocking, since everything internally is
          non-blocking */
@@ -719,7 +823,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
       else if(rc) {
         failf(data, "Failure establishing ssh session");
         state(conn, SSH_SESSION_FREE);
-        sshc->actualcode = CURLE_FAILED_INIT;
+        ssh_set_code_and_ssh_error(conn, CURLE_FAILED_INIT, rc);
         break;
       }
 
@@ -760,7 +864,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         }
         else {
           state(conn, SSH_SESSION_FREE);
-          sshc->actualcode = libssh2_session_error_to_CURLE(err);
+          ssh_set_code_from_ssh_error(conn, err);
           break;
         }
       }
@@ -802,7 +906,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         if(!rsa_pub_empty_but_ok && (sshc->rsa_pub == NULL)) {
           Curl_safefree(home);
           state(conn, SSH_SESSION_FREE);
-          sshc->actualcode = CURLE_OUT_OF_MEMORY;
+          ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
           break;
         }
 
@@ -818,7 +922,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           Curl_safefree(home);
           Curl_safefree(sshc->rsa_pub);
           state(conn, SSH_SESSION_FREE);
-          sshc->actualcode = CURLE_OUT_OF_MEMORY;
+          ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
           break;
         }
 
@@ -1031,7 +1135,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
       if(!sshc->authed) {
         failf(data, "Authentication failure");
         state(conn, SSH_SESSION_FREE);
-        sshc->actualcode = CURLE_LOGIN_DENIED;
+        ssh_set_code_and_ssh_error(conn, CURLE_LOGIN_DENIED, LIBSSH2_ERROR_NONE);
         break;
       }
 
@@ -1067,11 +1171,11 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         else {
           char *err_msg;
 
-          (void)libssh2_session_last_error(sshc->ssh_session,
+          int ssh_err = libssh2_session_last_error(sshc->ssh_session,
                                            &err_msg, NULL, 0);
           failf(data, "Failure initializing sftp session: %s", err_msg);
           state(conn, SSH_SESSION_FREE);
-          sshc->actualcode = CURLE_FAILED_INIT;
+          ssh_set_code_and_ssh_error(conn, CURLE_FAILED_INIT, ssh_err);
           break;
         }
       }
@@ -1096,7 +1200,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         sshc->homedir = strdup(tempHome);
         if(!sshc->homedir) {
           state(conn, SSH_SFTP_CLOSE);
-          sshc->actualcode = CURLE_OUT_OF_MEMORY;
+          ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
           break;
         }
         conn->data->state.most_recent_ftp_entrypath = sshc->homedir;
@@ -1104,10 +1208,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
       else {
         /* Return the error type */
         err = sftp_libssh2_last_error(sshc->sftp_session);
-        result = sftp_libssh2_error_to_CURLE(err);
-        sshc->actualcode = result?result:CURLE_SSH;
-        DEBUGF(infof(data, "error = %d makes libcurl = %d\n",
-                     err, (int)result));
+        result = ssh_set_code_from_ssh_error_with_default_code(conn, err, CURLE_SSH);
         state(conn, SSH_STOP);
         break;
       }
@@ -1124,7 +1225,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
 
       result = ssh_getworkingpath(conn, sshc->homedir, &sftp_scp->path);
       if(result) {
-        sshc->actualcode = result;
+        ssh_set_code_and_ssh_error(conn, result, LIBSSH2_ERROR_NONE);
         state(conn, SSH_STOP);
         break;
       }
@@ -1200,10 +1301,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
          */
         cp = strchr(cmd, ' ');
         if(cp == NULL) {
-          failf(data, "Syntax error in SFTP command. Supply parameter(s)!");
-          state(conn, SSH_SFTP_CLOSE);
-          sshc->nextstate = SSH_NO_STATE;
-          sshc->actualcode = CURLE_QUOTE_ERROR;
+          ssh_quote_fail_no_error(conn, "Syntax error in SFTP command. Supply parameter(s)!");
           break;
         }
 
@@ -1213,13 +1311,8 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
          */
         result = get_pathname(&cp, &sshc->quote_path1);
         if(result) {
-          if(result == CURLE_OUT_OF_MEMORY)
-            failf(data, "Out of memory");
-          else
-            failf(data, "Syntax error: Bad first parameter");
-          state(conn, SSH_SFTP_CLOSE);
-          sshc->nextstate = SSH_NO_STATE;
-          sshc->actualcode = result;
+            ssh_fail_with_result(conn, result,
+                "Syntax error: Bad first parameter");
           break;
         }
 
@@ -1238,15 +1331,8 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           /* get the destination */
           result = get_pathname(&cp, &sshc->quote_path2);
           if(result) {
-            if(result == CURLE_OUT_OF_MEMORY)
-              failf(data, "Out of memory");
-            else
-              failf(data, "Syntax error in chgrp/chmod/chown: "
-                    "Bad second parameter");
-            Curl_safefree(sshc->quote_path1);
-            state(conn, SSH_SFTP_CLOSE);
-            sshc->nextstate = SSH_NO_STATE;
-            sshc->actualcode = result;
+            ssh_fail_with_result(conn, result,
+                "Syntax error in chgrp/chmod/chown: Bad second parameter");
             break;
           }
           memset(&sshc->quote_attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
@@ -1260,15 +1346,8 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           /* get the destination */
           result = get_pathname(&cp, &sshc->quote_path2);
           if(result) {
-            if(result == CURLE_OUT_OF_MEMORY)
-              failf(data, "Out of memory");
-            else
-              failf(data,
-                    "Syntax error in ln/symlink: Bad second parameter");
-            Curl_safefree(sshc->quote_path1);
-            state(conn, SSH_SFTP_CLOSE);
-            sshc->nextstate = SSH_NO_STATE;
-            sshc->actualcode = result;
+            ssh_fail_with_result(conn, result,
+                "Syntax error in ln/symlink: Bad second parameter");
             break;
           }
           state(conn, SSH_SFTP_QUOTE_SYMLINK);
@@ -1285,14 +1364,8 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           /* second param is the dest. path */
           result = get_pathname(&cp, &sshc->quote_path2);
           if(result) {
-            if(result == CURLE_OUT_OF_MEMORY)
-              failf(data, "Out of memory");
-            else
-              failf(data, "Syntax error in rename: Bad second parameter");
-            Curl_safefree(sshc->quote_path1);
-            state(conn, SSH_SFTP_CLOSE);
-            sshc->nextstate = SSH_NO_STATE;
-            sshc->actualcode = result;
+              ssh_fail_with_result(conn, result,
+                "Syntax error in rename: Bad second parameter");
             break;
           }
           state(conn, SSH_SFTP_QUOTE_RENAME);
@@ -1308,12 +1381,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           break;
         }
 
-        failf(data, "Unknown SFTP command");
-        Curl_safefree(sshc->quote_path1);
-        Curl_safefree(sshc->quote_path2);
-        state(conn, SSH_SFTP_CLOSE);
-        sshc->nextstate = SSH_NO_STATE;
-        sshc->actualcode = CURLE_QUOTE_ERROR;
+        ssh_quote_fail_no_error(conn, "Unknown SFTP command");
         break;
       }
     }
@@ -1370,14 +1438,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           break;
         }
         else if(rc != 0 && !sshc->acceptfail) { /* get those attributes */
-          err = sftp_libssh2_last_error(sshc->sftp_session);
-          Curl_safefree(sshc->quote_path1);
-          Curl_safefree(sshc->quote_path2);
-          failf(data, "Attempt to get SFTP stats failed: %s",
-                sftp_libssh2_strerror(err));
-          state(conn, SSH_SFTP_CLOSE);
-          sshc->nextstate = SSH_NO_STATE;
-          sshc->actualcode = CURLE_QUOTE_ERROR;
+        ssh_quote_fail(conn, "Attempt to get SFTP stats failed: %s");
           break;
         }
       }
@@ -1388,12 +1449,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
         if(sshc->quote_attrs.gid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
            !sshc->acceptfail) {
-          Curl_safefree(sshc->quote_path1);
-          Curl_safefree(sshc->quote_path2);
-          failf(data, "Syntax error: chgrp gid not a number");
-          state(conn, SSH_SFTP_CLOSE);
-          sshc->nextstate = SSH_NO_STATE;
-          sshc->actualcode = CURLE_QUOTE_ERROR;
+            ssh_quote_fail_no_error(conn, "Syntax error: chgrp gid not a number");
           break;
         }
       }
@@ -1403,12 +1459,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         /* permissions are octal */
         if(sshc->quote_attrs.permissions == 0 &&
            !ISDIGIT(sshc->quote_path1[0])) {
-          Curl_safefree(sshc->quote_path1);
-          Curl_safefree(sshc->quote_path2);
-          failf(data, "Syntax error: chmod permissions not a number");
-          state(conn, SSH_SFTP_CLOSE);
-          sshc->nextstate = SSH_NO_STATE;
-          sshc->actualcode = CURLE_QUOTE_ERROR;
+            ssh_quote_fail_no_error(conn, "Syntax error: chmod permissions not a number");
           break;
         }
       }
@@ -1417,12 +1468,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
         if(sshc->quote_attrs.uid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
            !sshc->acceptfail) {
-          Curl_safefree(sshc->quote_path1);
-          Curl_safefree(sshc->quote_path2);
-          failf(data, "Syntax error: chown uid not a number");
-          state(conn, SSH_SFTP_CLOSE);
-          sshc->nextstate = SSH_NO_STATE;
-          sshc->actualcode = CURLE_QUOTE_ERROR;
+            ssh_quote_fail_no_error(conn, "Syntax error: chown uid not a number");
           break;
         }
       }
@@ -1441,14 +1487,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         break;
       }
       else if(rc != 0 && !sshc->acceptfail) {
-        err = sftp_libssh2_last_error(sshc->sftp_session);
-        Curl_safefree(sshc->quote_path1);
-        Curl_safefree(sshc->quote_path2);
-        failf(data, "Attempt to set SFTP stats failed: %s",
-              sftp_libssh2_strerror(err));
-        state(conn, SSH_SFTP_CLOSE);
-        sshc->nextstate = SSH_NO_STATE;
-        sshc->actualcode = CURLE_QUOTE_ERROR;
+          ssh_quote_fail(conn, "Attempt to set SFTP stats failed: %s");
         break;
       }
       state(conn, SSH_SFTP_NEXT_QUOTE);
@@ -1464,14 +1503,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         break;
       }
       else if(rc != 0 && !sshc->acceptfail) {
-        err = sftp_libssh2_last_error(sshc->sftp_session);
-        Curl_safefree(sshc->quote_path1);
-        Curl_safefree(sshc->quote_path2);
-        failf(data, "symlink command failed: %s",
-              sftp_libssh2_strerror(err));
-        state(conn, SSH_SFTP_CLOSE);
-        sshc->nextstate = SSH_NO_STATE;
-        sshc->actualcode = CURLE_QUOTE_ERROR;
+          ssh_quote_fail(conn, "symlink command failed: %s");
         break;
       }
       state(conn, SSH_SFTP_NEXT_QUOTE);
@@ -1485,12 +1517,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         break;
       }
       else if(rc != 0 && !sshc->acceptfail) {
-        err = sftp_libssh2_last_error(sshc->sftp_session);
-        Curl_safefree(sshc->quote_path1);
-        failf(data, "mkdir command failed: %s", sftp_libssh2_strerror(err));
-        state(conn, SSH_SFTP_CLOSE);
-        sshc->nextstate = SSH_NO_STATE;
-        sshc->actualcode = CURLE_QUOTE_ERROR;
+          ssh_quote_fail(conn, "mkdir command failed: %s");
         break;
       }
       state(conn, SSH_SFTP_NEXT_QUOTE);
@@ -1509,13 +1536,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         break;
       }
       else if(rc != 0 && !sshc->acceptfail) {
-        err = sftp_libssh2_last_error(sshc->sftp_session);
-        Curl_safefree(sshc->quote_path1);
-        Curl_safefree(sshc->quote_path2);
-        failf(data, "rename command failed: %s", sftp_libssh2_strerror(err));
-        state(conn, SSH_SFTP_CLOSE);
-        sshc->nextstate = SSH_NO_STATE;
-        sshc->actualcode = CURLE_QUOTE_ERROR;
+          ssh_quote_fail(conn, "rename command failed: %s");
         break;
       }
       state(conn, SSH_SFTP_NEXT_QUOTE);
@@ -1528,12 +1549,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         break;
       }
       else if(rc != 0 && !sshc->acceptfail) {
-        err = sftp_libssh2_last_error(sshc->sftp_session);
-        Curl_safefree(sshc->quote_path1);
-        failf(data, "rmdir command failed: %s", sftp_libssh2_strerror(err));
-        state(conn, SSH_SFTP_CLOSE);
-        sshc->nextstate = SSH_NO_STATE;
-        sshc->actualcode = CURLE_QUOTE_ERROR;
+          ssh_quote_fail(conn, "rmdir command failed: %s");
         break;
       }
       state(conn, SSH_SFTP_NEXT_QUOTE);
@@ -1546,12 +1562,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         break;
       }
       else if(rc != 0 && !sshc->acceptfail) {
-        err = sftp_libssh2_last_error(sshc->sftp_session);
-        Curl_safefree(sshc->quote_path1);
-        failf(data, "rm command failed: %s", sftp_libssh2_strerror(err));
-        state(conn, SSH_SFTP_CLOSE);
-        sshc->nextstate = SSH_NO_STATE;
-        sshc->actualcode = CURLE_QUOTE_ERROR;
+          ssh_quote_fail(conn, "rm command failed: %s");
         break;
       }
       state(conn, SSH_SFTP_NEXT_QUOTE);
@@ -1634,8 +1645,10 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
 
           if(sshc->secondCreateDirs) {
             state(conn, SSH_SFTP_CLOSE);
-            sshc->actualcode = err>= LIBSSH2_FX_OK?
-              sftp_libssh2_error_to_CURLE(err):CURLE_SSH;
+              if (err >= LIBSSH2_FX_OK)
+                  ssh_set_code_from_ssh_error(conn, err);
+              else
+                  ssh_set_code_and_ssh_error(conn, CURLE_SSH, err);
             failf(data, "Creating the dir/file failed: %s",
                   sftp_libssh2_strerror(err));
             break;
@@ -1651,13 +1664,15 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
             break;
           }
           state(conn, SSH_SFTP_CLOSE);
-          sshc->actualcode = err>= LIBSSH2_FX_OK?
-            sftp_libssh2_error_to_CURLE(err):CURLE_SSH;
+          if (err >= LIBSSH2_FX_OK)
+            ssh_set_code_from_ssh_error(conn, err);
+          else
+            ssh_set_code_and_ssh_error(conn, CURLE_SSH, err);
           if(!sshc->actualcode) {
             /* Sometimes, for some reason libssh2_sftp_last_error() returns
                zero even though libssh2_sftp_open() failed previously! We need
                to work around that! */
-            sshc->actualcode = CURLE_SSH;
+            ssh_set_code_and_ssh_error(conn, CURLE_SSH, err);
             err=-1;
           }
           failf(data, "Upload failed: %s (%d/%d)",
@@ -1726,7 +1741,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
 
       if(result) {
         state(conn, SSH_SFTP_CLOSE);
-        sshc->actualcode = result;
+        ssh_set_code_and_ssh_error(conn, result, LIBSSH2_ERROR_NONE);
       }
       else {
         /* store this original bitmask setup to use later on if we can't
@@ -1791,9 +1806,8 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         if((err != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
            (err != LIBSSH2_FX_FAILURE) &&
            (err != LIBSSH2_FX_PERMISSION_DENIED)) {
-          result = sftp_libssh2_error_to_CURLE(err);
           state(conn, SSH_SFTP_CLOSE);
-          sshc->actualcode = result?result:CURLE_SSH;
+          result = ssh_set_code_from_ssh_error_with_default_code(conn, err, CURLE_SSH);
           break;
         }
       }
@@ -1821,20 +1835,19 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           failf(data, "Could not open directory for reading: %s",
                 sftp_libssh2_strerror(err));
           state(conn, SSH_SFTP_CLOSE);
-          result = sftp_libssh2_error_to_CURLE(err);
-          sshc->actualcode = result?result:CURLE_SSH;
+          result = ssh_set_code_from_ssh_error_with_default_code(conn, err, CURLE_SSH);
           break;
         }
       }
       if((sshc->readdir_filename = malloc(PATH_MAX+1)) == NULL) {
         state(conn, SSH_SFTP_CLOSE);
-        sshc->actualcode = CURLE_OUT_OF_MEMORY;
+        ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
         break;
       }
       if((sshc->readdir_longentry = malloc(PATH_MAX+1)) == NULL) {
         Curl_safefree(sshc->readdir_filename);
         state(conn, SSH_SFTP_CLOSE);
-        sshc->actualcode = CURLE_OUT_OF_MEMORY;
+        ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
         break;
       }
       state(conn, SSH_SFTP_READDIR);
@@ -1860,7 +1873,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           tmpLine = aprintf("%s\n", sshc->readdir_filename);
           if(tmpLine == NULL) {
             state(conn, SSH_SFTP_CLOSE);
-            sshc->actualcode = CURLE_OUT_OF_MEMORY;
+            ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
             break;
           }
           result = Curl_client_write(conn, CLIENTWRITE_BODY,
@@ -1889,7 +1902,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
             Curl_safefree(sshc->readdir_filename);
             Curl_safefree(sshc->readdir_longentry);
             state(conn, SSH_SFTP_CLOSE);
-            sshc->actualcode = CURLE_OUT_OF_MEMORY;
+            ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
             break;
           }
 
@@ -1903,7 +1916,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
               Curl_safefree(sshc->readdir_filename);
               Curl_safefree(sshc->readdir_longentry);
               state(conn, SSH_SFTP_CLOSE);
-              sshc->actualcode = CURLE_OUT_OF_MEMORY;
+              ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
               break;
             }
 
@@ -1923,9 +1936,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         break;
       }
       else if(sshc->readdir_len <= 0) {
-        err = sftp_libssh2_last_error(sshc->sftp_session);
-        result = sftp_libssh2_error_to_CURLE(err);
-        sshc->actualcode = result?result:CURLE_SSH;
+        result = ssh_set_code_from_ssh_error_with_default_code(conn, err, CURLE_SSH);
         failf(data, "Could not open remote file for reading: %s :: %d",
               sftp_libssh2_strerror(err),
               libssh2_session_last_errno(sshc->ssh_session));
@@ -1957,7 +1968,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
         Curl_safefree(sshc->readdir_filename);
         Curl_safefree(sshc->readdir_longentry);
         state(conn, SSH_SFTP_CLOSE);
-        sshc->actualcode = CURLE_OUT_OF_MEMORY;
+        ssh_set_code_and_ssh_error(conn, CURLE_OUT_OF_MEMORY, LIBSSH2_ERROR_NONE);
         break;
       }
       sshc->readdir_line = new_readdir_line;
@@ -2033,8 +2044,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
           failf(data, "Could not open remote file for reading: %s",
                 sftp_libssh2_strerror(err));
           state(conn, SSH_SFTP_CLOSE);
-          result = sftp_libssh2_error_to_CURLE(err);
-          sshc->actualcode = result?result:CURLE_SSH;
+          result = ssh_set_code_from_ssh_error_with_default_code(conn, err, CURLE_SSH);
           break;
         }
       }
@@ -2156,7 +2166,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
     }
     if(result) {
       state(conn, SSH_SFTP_CLOSE);
-      sshc->actualcode = result;
+      ssh_set_code_and_ssh_error(conn, result, LIBSSH2_ERROR_NONE);
     }
     else {
       state(conn, SSH_STOP);
@@ -2227,7 +2237,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
     case SSH_SCP_TRANS_INIT:
       result = ssh_getworkingpath(conn, sshc->homedir, &sftp_scp->path);
       if(result) {
-        sshc->actualcode = result;
+        ssh_set_code_and_ssh_error(conn, result, LIBSSH2_ERROR_NONE);
         state(conn, SSH_STOP);
         break;
       }
@@ -2235,7 +2245,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
       if(data->set.upload) {
         if(data->set.infilesize < 0) {
           failf(data, "SCP requires a known file size for upload");
-          sshc->actualcode = CURLE_UPLOAD_FAILED;
+          ssh_set_code_and_ssh_error(conn, CURLE_UPLOAD_FAILED, LIBSSH2_ERROR_NONE);
           state(conn, SSH_SCP_CHANNEL_FREE);
           break;
         }
@@ -2270,7 +2280,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
                                                      &err_msg, NULL, 0));
           failf(conn->data, "%s", err_msg);
           state(conn, SSH_SCP_CHANNEL_FREE);
-          sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
+          ssh_set_code_from_ssh_error(conn, ssh_err);
           break;
         }
       }
@@ -2284,7 +2294,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
 
       if(result) {
         state(conn, SSH_SCP_CHANNEL_FREE);
-        sshc->actualcode = result;
+        ssh_set_code_and_ssh_error(conn, result, LIBSSH2_ERROR_NONE);
       }
       else {
         /* we want to use the _sending_ function even when the socket turns
@@ -2325,7 +2335,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
                                                      &err_msg, NULL, 0));
           failf(conn->data, "%s", err_msg);
           state(conn, SSH_SCP_CHANNEL_FREE);
-          sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
+          ssh_set_code_from_ssh_error(conn, ssh_err);
           break;
         }
       }
@@ -2345,7 +2355,7 @@ static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
 
       if(result) {
         state(conn, SSH_SCP_CHANNEL_FREE);
-        sshc->actualcode = result;
+        ssh_set_code_and_ssh_error(conn, result, LIBSSH2_ERROR_NONE);
       }
       else
         state(conn, SSH_STOP);
@@ -2695,7 +2705,7 @@ static CURLcode ssh_init(struct connectdata *conn)
   struct SSHPROTO *ssh;
   struct ssh_conn *sshc = &conn->proto.sshc;
 
-  sshc->actualcode = CURLE_OK; /* reset error code */
+  ssh_set_code_and_ssh_error(conn, CURLE_OK, LIBSSH2_ERROR_NONE); /* reset error code */
   sshc->secondCreateDirs =0;   /* reset the create dir attempt state
                                   variable */
 
@@ -2969,6 +2979,7 @@ static ssize_t scp_send(struct connectdata *conn, int sockindex,
   }
   else if(nwrite < LIBSSH2_ERROR_NONE) {
     *err = libssh2_session_error_to_CURLE((int)nwrite);
+    conn->data->info.httpcode = nwrite;
     nwrite = -1;
   }
 
@@ -3112,6 +3123,7 @@ static ssize_t sftp_send(struct connectdata *conn, int sockindex,
   }
   else if(nwrite < LIBSSH2_ERROR_NONE) {
     *err = libssh2_session_error_to_CURLE((int)nwrite);
+    conn->data->info.httpcode = nwrite;
     nwrite = -1;
   }
 

