diff -uNr opennebula-4.12.0.orig/src/sunstone/public/js/plugins/vms-tab.js opennebula-4.12.0-vncoversameport/src/sunstone/public/js/plugins/vms-tab.js
--- opennebula-4.12.0.orig/src/sunstone/public/js/plugins/vms-tab.js	2015-03-09 13:58:29.000000000 -0400
+++ opennebula-4.12.0-vncoversameport/src/sunstone/public/js/plugins/vms-tab.js	2015-03-17 02:49:12.579664536 -0400
@@ -3346,25 +3346,33 @@
 
 function vncCallback(request,response){
     rfb = new RFB({'target':       $D('VNC_canvas'),
-                   'encrypt':      config['user_config']['vnc_wss'] == "yes",
+                   'encrypt':      window.location.protocol === 'https:',
                    'true_color':   true,
                    'local_cursor': true,
                    'shared':       true,
                    'onUpdateState':  updateVNCState});
 
     var proxy_host = window.location.hostname;
-    var proxy_port = config['system_config']['vnc_proxy_port'];
+    var proxy_port = window.location.port;
+
+    // if port == 80 (or 443) then it won't be present and should be
+    // set manually
+    if (!proxy_port) {
+        if (window.location.protocol === 'http:') {
+            proxy_port = 80;
+        } else if (window.location.protocol === 'https:') {
+            proxy_port = 443;
+        }
+    }
+
     var pw = response["password"];
     var token = response["token"];
     var vm_name = response["vm_name"];
     var path = '?token='+token;
 
     var url = "vnc?";
-    url += "host=" + proxy_host;
-    url += "&port=" + proxy_port;
-    url += "&token=" + token;
+    url += "token=" + token;
     url += "&password=" + pw;
-    url += "&encrypt=" + config['user_config']['vnc_wss'];
     url += "&title=" + vm_name;
 
     $("#open_in_a_new_window").attr('href', url)
@@ -3402,12 +3410,22 @@
 
     var host, port, password, scheme = "ws://", uri, token, vm_name;
 
-    if (config['user_config']['vnc_wss'] == "yes") {
+    if (window.location.protocol === 'https:') {
         scheme = "wss://";
     }
 
     host = window.location.hostname;
-    port = config['system_config']['vnc_proxy_port'];
+    port = window.location.port;
+    // if port == 80 (or 443) then it won't be present and should be
+    // set manually
+    if (!port) {
+        if (window.location.protocol === 'http:') {
+            port = 80;
+        } else if (window.location.protocol === 'https:') {
+            port = 443;
+        }
+    }
+
     password = response["password"];
     token = response["token"];
     vm_name = response["vm_name"];
@@ -3433,11 +3451,8 @@
     }
 
     var url = "spice?";
-    url += "host=" + host;
-    url += "&port=" + port;
-    url += "&token=" + token;
+    url += "token=" + token;
     url += "&password=" + password;
-    url += "&encrypt=" + config['user_config']['vnc_wss'];
     url += "&title=" + vm_name;
 
     $("#open_in_a_new_window_spice").attr('href', url)
diff -uNr opennebula-4.12.0.orig/src/sunstone/views/vnc.erb opennebula-4.12.0-vncoversameport/src/sunstone/views/vnc.erb
--- opennebula-4.12.0.orig/src/sunstone/views/vnc.erb	2015-03-09 13:58:29.000000000 -0400
+++ opennebula-4.12.0-vncoversameport/src/sunstone/views/vnc.erb	2015-03-17 02:44:05.891678029 -0400
@@ -169,10 +169,9 @@
             // if port == 80 (or 443) then it won't be present and should be
             // set manually
             if (!port) {
-                if (window.location.protocol.substring(0,4) == 'http') {
+                if (window.location.protocol === 'http:') {
                     port = 80;
-                }
-                else if (window.location.protocol.substring(0,5) == 'https') {
+                } else if (window.location.protocol === 'https:') {
                     port = 443;
                 }
             }
@@ -205,7 +204,7 @@
                            'onXvpInit':    xvpInit,
                            'onPasswordRequired':  passwordRequired});
 
-            rfb.connect("<%= params['host']%>", "<%= params['port']%>", "<%= params['password']%>", path+"?token=<%= params['token']%>");
+            rfb.connect(host, port, "<%= params['password']%>", path+"?token=<%= params['token']%>");
         };
 
         </script>
