diff --git a/c/httphead/httphead.c b/c/httphead/httphead.c
index f446d8d..349d617 100644
--- a/c/httphead/httphead.c
+++ b/c/httphead/httphead.c
@@ -36,9 +36,6 @@
 #include <errno.h>
 #include <string.h>
 
-// for debugging only
-#include <stdio.h>
-
 const char usagemsg[] =
 	"usage: httphead [-r] [-q] [-u username:password] [-p port] [-s host] [-d path] [-s host] URL\n"
 	"\n"
@@ -98,6 +95,7 @@
 char* getuser ( const char* url );
 char* getpassword ( const char* url );
 char* gethost ( const char* url );
+char* getport ( const char* url );
 char* getpath ( const char* url );
 char* getstatuscode ( const char* response );
 char* b64 ( const char* str );
@@ -115,7 +113,7 @@
 	char* path = NULL;
 	char* username = NULL;
 	char* password = NULL;
-	int port = 80;
+	char* port = NULL;
 
 	int showrequest = 0;
 	int statuscodeonly = 0;
@@ -139,7 +137,7 @@
 				version();
 				exit(0);
 			case 'p':
-				port = atoi(optarg);
+				port = optarg;
 				break;
 			case 's':
 				host = optarg;
@@ -188,6 +186,10 @@
 		host = gethost(url);
 	}
 
+	if ( port == NULL ) {
+		port = getport(url);
+	}
+
 	if ( username == NULL ) {
 		username = getuser(url);
 	}
@@ -229,7 +231,11 @@
 	remote_host_addr = alloca(sizeof(struct sockaddr_in));
 	remote_host_addr->sin_family = AF_INET;
 	remote_host_addr->sin_addr = *((struct in_addr*) remote_hostent->h_addr);
-	remote_host_addr->sin_port = port;
+	if ( port == NULL ) {
+		remote_host_addr->sin_port = 80;
+	} else {
+		remote_host_addr->sin_port = atoi(port);
+	}
 
 	if ( (sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1 ) {
 		showerror("socket");
@@ -251,6 +257,10 @@
 		crlf(1);
 		write(1, "Host: ", 6);
 		write(1, host, strlen(host));
+		if ( port != NULL ) {
+			write(1, ":", 1);
+			write(1, port, strlen(port));
+		}
 		crlf(1);
 
 		if ((username != NULL) || (password != NULL)) {
@@ -270,6 +280,10 @@
 	crlf(sock);
 	write(sock, "Host: ", 6);
 	write(sock, host, strlen(host));
+	if ( port != NULL ) {
+		write(sock, ":", 1);
+		write(sock, port, strlen(port));
+	}
 	crlf(sock);
 
 	if ((username != NULL) || (password != NULL)) {
@@ -324,7 +338,7 @@
 	if ( s == NULL ) {
 		s = strstr(url, "https://");
 		if ( s == NULL ) {
-			s = url; // assume that there is no http[s]:// prefix
+			s = (char*)url; // assume that there is no http[s]:// prefix
 		} else {
 			s += 8;
 		}
@@ -333,7 +347,7 @@
 	}
 
 	if ( s >= url + strlen(url) ) {
-		return url;
+		return (char*)url;
 	}
 
 	return s;
@@ -392,8 +406,8 @@
 	char* s = NULL;
 	char* r = NULL;
 	int e = 0;
-	int a = 0;
-	int p = 0;
+	char* a = NULL;
+	char* p = NULL;
 
 	s = _gethoststart(url);
 
@@ -424,8 +438,8 @@
 	char* s = NULL;
 	char* r = NULL;
 	int e = 0;
-	int a = 0;
-	int p = 0;
+	char* a = NULL;
+	char* p = NULL;
 
 	s = _gethoststart(url);
 
@@ -460,7 +474,8 @@
 	char* s = NULL;
 	char* r = NULL;
 	int e = 0;
-	int a = 0;
+	char* a = NULL;
+	char* p = NULL;
 
 	s = _gethoststart(url);
 
@@ -473,10 +488,16 @@
 	a = strchr(s, '@');
 
 	if ((a != NULL) && (a < s+e)) {
-		e = ((int)(s+e) - (int)a - 2);
+		e = ((int)(s+e) - (int)a - 1);
 		s = a+1;
 	}
 
+	p = strchr(s, ':');
+
+	if ((p != NULL) && ((a == NULL) || (p>a)) && (p < s+e)) {
+		e = (int)p-(int)s;
+	}
+
 	r = (char*)malloc(sizeof(char)*(e+1));
 
 	memcpy(r, s, e);
@@ -486,6 +507,47 @@
 	return r;
 }
 
+char* getport ( const char* url ) {
+	char* s = NULL;
+	char* r = NULL;
+	int e = 0;
+	char* a = NULL;
+	char* p = NULL;
+
+	s = _gethoststart(url);
+
+	e = _gethostlen(s);
+
+	if ( e == 0 ) {
+		return NULL;
+	}
+
+	a = strchr(s, '@');
+
+	if ((a != NULL) && (a < s+e)) {
+		e = ((int)(s+e) - (int)a - 1);
+		s = a+1;
+	}
+
+	p = strchr(s, ':');
+
+	if ((p != NULL) && ((a == NULL) || (p>a)) && (p < s+e-1)) {
+		p++;
+		e = e - ((int)p-(int)s);
+
+		r = (char*)malloc(sizeof(char)*(e+1));
+
+		memcpy(r, p, e);
+
+		r[e+1] = '\0';
+
+		return r;
+	} else {
+		return NULL;
+	}
+
+}
+
 void _b64_43 ( char* dst, const char* src ) {
 	dst[0] = b64chars[src[0] >> 2];
 	dst[1] = b64chars[(((src[0] & 0x03) << 4) & 0xF0) | (src[1] >> 4)];