Index: trunk/Server/Makefile
===================================================================
--- trunk/Server/Makefile	(revision 209)
+++ trunk/Server/Makefile	(revision 212)
@@ -1,10 +1,12 @@
 # $Id$
 
+OBJ=o
+STATIC=a
 include $(PWD)/Platform/$(PLATFORM).mk
 
 .PHONY: all clean
-.SUFFIXES: .c .o
+.SUFFIXES: .c .$(OBJ)
 
-OBJS = main.o version.o config.o server.o http.o module.o strptime.o font.o $(EXTOBJS) $(PREOBJS)
+OBJS = main.$(OBJ) version.$(OBJ) config.$(OBJ) server.$(OBJ) http.$(OBJ) module.$(OBJ) strptime.$(OBJ) font.$(OBJ) $(EXTOBJS) $(PREOBJS)
 
 all: tewi$(EXEC) $(TARGET)
@@ -12,6 +14,6 @@
 tewi_strip$(EXEC): tewi$(EXEC)
 
-tewi$(EXEC): $(OBJS) ../Common/common.a
-	$(CC) $(LDFLAGS) $(EXTLDFLAGS) -o $@ $(OBJS) $(EXTLIBS) ../Common/common.a $(LIBS)
+tewi$(EXEC): $(OBJS) ../Common/common.$(STATIC)
+	$(CC) $(LDFLAGS) $(EXTLDFLAGS) -o $@ $(OBJS) $(EXTLIBS) ../Common/common.$(STATIC) $(LIBS)
 	$(SERVADD)
 
@@ -41,5 +43,5 @@
 	package_finalize $@
 
-.c.o:
+.c.$(OBJ):
 	$(CC) $(CFLAGS) $(EXTCFLAGS) -c -o $@ $<
 
@@ -48,3 +50,3 @@
 
 clean:
-	rm -f *.o tewi *.exe *.res *.elf *.sfo *.pbp *.self *.pkg
+	rm -f *.o tewi *.exe *.res *.elf *.sfo *.pbp *.self *.pkg *.obj
Index: trunk/Server/config.c
===================================================================
--- trunk/Server/config.c	(revision 209)
+++ trunk/Server/config.c	(revision 212)
@@ -10,7 +10,10 @@
 #include <stdlib.h>
 #include <string.h>
+
+#ifndef _MSC_VER
 #include <unistd.h>
-
-#ifdef __MINGW32__
+#endif
+
+#if defined(__MINGW32__) || defined(_MSC_VER)
 #include <winsock2.h>
 #endif
@@ -38,9 +41,10 @@
 again:
 	for(i = 0; i < vhost->dir_count; i++) {
+		char* noslash;
 		struct tw_dir_entry* e = &vhost->dirs[i];
 		pathstart = false;
 		if(strlen(path) >= strlen(e->dir)) {
+			int j;
 			pathstart = true;
-			int j;
 			for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {
 				if(path[j] != e->dir[j]) {
@@ -50,5 +54,5 @@
 			}
 		}
-		char* noslash = cm_strdup(e->dir);
+		noslash = cm_strdup(e->dir);
 		noslash[strlen(noslash) - 1] = 0;
 		if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {
@@ -120,23 +124,24 @@
 
 int tw_config_read(const char* path) {
-	cm_log("Config", "Reading %s", path);
 	char cbuf[2];
-	cbuf[1] = 0;
 	int ln = 0;
 	int ifbr = 0;
 	int ignore = -1;
-	FILE* f = fopen(path, "r");
+	FILE* f;
+	cm_log("Config", "Reading %s", path);
+	f = fopen(path, "r");
+	cbuf[1] = 0;
 	if(f != NULL) {
 		char* line = malloc(1);
-		line[0] = 0;
 		int stop = 0;
 		struct tw_config_entry* current = &config.root;
 		char* vhost = NULL;
 		char* dir = NULL;
+		line[0] = 0;
 		while(stop == 0) {
 			int c = fread(cbuf, 1, 1, f);
 			if(cbuf[0] == '\n' || c <= 0) {
+				char* l = cm_trim(line);
 				ln++;
-				char* l = cm_trim(line);
 				if(strlen(l) > 0 && l[0] != '#') {
 					char** r = cm_split(l, " \t");
@@ -227,4 +232,5 @@
 								stop = 1;
 							} else {
+								int i;
 								vhost = cm_strdup(r[1]);
 								current = &config.vhosts[config.vhost_count++];
@@ -235,5 +241,4 @@
 								current->readme_count = 0;
 								current->hideport = -1;
-								int i;
 								current->name = cm_strdup(vhost);
 								current->port = -1;
@@ -262,8 +267,16 @@
 					) {
 						for(i = 1; r[i] != NULL; i++) {
+#ifdef _MSC_VER
+							uint32_t port = atoi(r[i]);
+#else
 							uint64_t port = atoi(r[i]);
+#endif
+							int j;
 							cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
-							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
-							int j;
+#ifdef _MSC_VER
+							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1UL << 31);
+#else
+							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 31);
+#endif
 							for(j = 0; config.ports[j] != -1; j++)
 								;
@@ -319,6 +332,6 @@
 							cm_log("Config", "Missing condition type at line %d", ln);
 						} else {
+							bool ign = false;
 							ifbr++;
-							bool ign = false;
 							if(cm_strcaseequ(r[1], "False")) {
 								ign = true;
@@ -429,10 +442,10 @@
 						if(r[0] != NULL) {
 							int argc;
+							int i;
+							bool called = false;
+							struct tw_tool tools;
 							for(argc = 0; r[argc] != NULL; argc++)
 								;
 							stop = 0;
-							int i;
-							bool called = false;
-							struct tw_tool tools;
 							tw_init_tools(&tools);
 							for(i = 0; i < config.module_count; i++) {
Index: trunk/Server/http.c
===================================================================
--- trunk/Server/http.c	(revision 209)
+++ trunk/Server/http.c	(revision 212)
@@ -16,5 +16,5 @@
 #include <string.h>
 
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 #include <winsock2.h>
 #else
@@ -54,4 +54,6 @@
 	char cbuf[2];
 	int phase = 0;
+	char* header;
+	int nl;
 
 #ifdef USE_POLL
@@ -74,13 +76,16 @@
 	req->version = NULL;
 
-	char* header = malloc(1);
+	header = malloc(1);
 	header[0] = 0;
-	int nl = 0;
+	nl = 0;
 
 	while(1) {
+		int i;
+		int len;
+			int n;
 #ifndef USE_POLL
+		struct timeval tv;
 		FD_ZERO(&fds);
 		FD_SET(sock, &fds);
-		struct timeval tv;
 		tv.tv_sec = 5;
 		tv.tv_usec = 0;
@@ -90,10 +95,10 @@
 #endif
 #ifdef USE_POLL
-			int n = poll(pollfds, 1, 5000);
+			n = poll(pollfds, 1, 5000);
 #else
 #ifdef __HAIKU__
-		int n = select(32, &fds, NULL, NULL, &tv);
-#else
-		int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
+		n = select(32, &fds, NULL, NULL, &tv);
+#else
+		n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
 #endif
 #endif
@@ -107,10 +112,9 @@
 		}
 #endif
-		int len = tw_read(ssl, sock, buffer, 512);
+		len = tw_read(ssl, sock, buffer, 512);
 		if(len <= 0) {
 			bad = true;
 			break;
 		}
-		int i;
 		for(i = 0; i < len; i++) {
 			char c = buffer[i];
@@ -125,4 +129,5 @@
 					}
 				} else {
+					char* tmp;
 					if(req->method == NULL) {
 						req->method = malloc(1);
@@ -130,5 +135,5 @@
 					}
 					cbuf[0] = c;
-					char* tmp = req->method;
+					tmp = req->method;
 					req->method = cm_strcat(tmp, cbuf);
 					free(tmp);
@@ -144,4 +149,5 @@
 					}
 				} else {
+					char* tmp;
 					if(req->path == NULL) {
 						req->path = malloc(1);
@@ -149,5 +155,5 @@
 					}
 					cbuf[0] = c;
-					char* tmp = req->path;
+					tmp = req->path;
 					req->path = cm_strcat(tmp, cbuf);
 					free(tmp);
@@ -161,5 +167,7 @@
 					} else {
 						/* We have Method, Path, Version now */
-
+						int j;
+						char* p;
+						int incr;
 						if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) {
 							cm_log("HTTP", "Bad HTTP Version");
@@ -168,8 +176,8 @@
 						}
 
-						int j;
-						char* p = malloc(1);
+						p = malloc(1);
 						p[0] = 0;
 						for(j = 0; req->path[j] != 0; j++) {
+							char* tmp;
 							if(req->path[j] == '/') {
 								cbuf[0] = '/';
@@ -180,5 +188,5 @@
 								cbuf[0] = req->path[j];
 							}
-							char* tmp = p;
+							tmp = p;
 							p = cm_strcat(tmp, cbuf);
 							free(tmp);
@@ -187,5 +195,5 @@
 						req->path = p;
 
-						int incr = 0;
+						incr = 0;
 						p = malloc(1);
 						p[0] = 0;
@@ -193,8 +201,9 @@
 							if(req->path[j] == '/' || req->path[j] == 0) {
 								char oldc = req->path[j];
+								char* pth;
 								cbuf[0] = oldc;
 								req->path[j] = 0;
 
-								char* pth = req->path + incr;
+								pth = req->path + incr;
 
 								if(strcmp(pth, "..") == 0) {
@@ -230,4 +239,5 @@
 					}
 				} else if(c != '\r') {
+					char* tmp;
 					if(req->version == NULL) {
 						req->version = malloc(1);
@@ -235,5 +245,5 @@
 					}
 					cbuf[0] = c;
-					char* tmp = req->version;
+					tmp = req->version;
 					req->version = cm_strcat(tmp, cbuf);
 					free(tmp);
@@ -246,20 +256,23 @@
 						goto getout;
 					} else {
+						int j;
 						if(req->headers == NULL) {
 							req->headers = malloc(sizeof(*req->headers));
 							req->headers[0] = NULL;
 						}
-						int j;
 						for(j = 0; header[j] != 0; j++) {
 							if(header[j] == ':') {
+								char* kv;
+								char* vv;
+								char** old;
+								int k;
 								header[j] = 0;
 								j++;
 								for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
 									;
-								char* kv = header;
-								char* vv = header + j;
-
-								char** old = req->headers;
-								int k;
+								kv = header;
+								vv = header + j;
+
+								old = req->headers;
 								for(k = 0; old[k] != NULL; k++)
 									;
@@ -281,7 +294,8 @@
 					}
 				} else if(c != '\r') {
+					char* tmp;
 					nl = 0;
 					cbuf[0] = c;
-					char* tmp = header;
+					tmp = header;
 					header = cm_strcat(tmp, cbuf);
 					free(tmp);
@@ -296,72 +310,78 @@
 		return 1;
 	}
-	char* result = malloc(1);
-	result[0] = 0;
-	int i;
-	for(i = 0; req->path[i] != 0; i++) {
-		if(req->path[i] == '?') {
-			req->path[i] = 0;
-			req->query = cm_strdup(req->path + i + 1);
-			break;
-		}
-	}
-	for(i = 0; req->path[i] != 0; i++) {
-		if(req->path[i] == '%') {
-			if(req->path[i + 1] == 0) continue;
-			cbuf[0] = cm_hex(req->path + i + 1, 2);
-			if(cbuf[0] != '\\') {
-				char* tmp = result;
+	{
+		char* result = malloc(1);
+		int i;
+		int j;
+		int incr;
+		char* p;
+		result[0] = 0;
+		for(i = 0; req->path[i] != 0; i++) {
+			if(req->path[i] == '?') {
+				req->path[i] = 0;
+				req->query = cm_strdup(req->path + i + 1);
+				break;
+			}
+		}
+		for(i = 0; req->path[i] != 0; i++) {
+			if(req->path[i] == '%') {
+				if(req->path[i + 1] == 0) continue;
+				cbuf[0] = cm_hex(req->path + i + 1, 2);
+				if(cbuf[0] != '\\') {
+					char* tmp = result;
+					result = cm_strcat(tmp, cbuf);
+					free(tmp);
+				}
+				i += 2;
+			} else if(req->path[i] != '\\') {
+				char* tmp;
+				cbuf[0] = req->path[i];
+				tmp = result;
 				result = cm_strcat(tmp, cbuf);
 				free(tmp);
 			}
-			i += 2;
-		} else if(req->path[i] != '\\') {
-			cbuf[0] = req->path[i];
-			char* tmp = result;
-			result = cm_strcat(tmp, cbuf);
-			free(tmp);
-		}
+		}
+		free(req->path);
+		req->path = result;
+	
+		incr = 0;
+		p = malloc(1);
+		p[0] = 0;
+		for(j = 0;; j++) {
+			if(req->path[j] == '/' || req->path[j] == 0) {
+				char oldc = req->path[j];
+				char* pth;
+				cbuf[0] = oldc;
+				req->path[j] = 0;
+	
+				pth = req->path + incr;
+	
+				if(strcmp(pth, "..") == 0) {
+					int k;
+					if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
+					for(k = strlen(p) - 1; k >= 0; k--) {
+						if(p[k] == '/') {
+							p[k + 1] = 0;
+							break;
+						}
+					}
+					if(strlen(p) == 0) {
+						free(p);
+						p = cm_strdup("/");
+					}
+				} else if(strcmp(pth, ".") == 0) {
+				} else {
+					char* tmp = p;
+					p = cm_strcat3(tmp, pth, cbuf);
+					free(tmp);
+				}
+	
+				incr = j + 1;
+				if(oldc == 0) break;
+			}
+		}
+		free(req->path);
+		req->path = p;
+		return 0;
 	}
-	free(req->path);
-	req->path = result;
-
-	int incr = 0;
-	char* p = malloc(1);
-	p[0] = 0;
-	int j;
-	for(j = 0;; j++) {
-		if(req->path[j] == '/' || req->path[j] == 0) {
-			char oldc = req->path[j];
-			cbuf[0] = oldc;
-			req->path[j] = 0;
-
-			char* pth = req->path + incr;
-
-			if(strcmp(pth, "..") == 0) {
-				int k;
-				if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
-				for(k = strlen(p) - 1; k >= 0; k--) {
-					if(p[k] == '/') {
-						p[k + 1] = 0;
-						break;
-					}
-				}
-				if(strlen(p) == 0) {
-					free(p);
-					p = cm_strdup("/");
-				}
-			} else if(strcmp(pth, ".") == 0) {
-			} else {
-				char* tmp = p;
-				p = cm_strcat3(tmp, pth, cbuf);
-				free(tmp);
-			}
-
-			incr = j + 1;
-			if(oldc == 0) break;
-		}
-	}
-	free(req->path);
-	req->path = p;
-	return 0;
 }
Index: trunk/Server/main.c
===================================================================
--- trunk/Server/main.c	(revision 209)
+++ trunk/Server/main.c	(revision 212)
@@ -5,5 +5,7 @@
 #include "../config.h"
 
+#ifndef _MSC_VER
 #include <unistd.h>
+#endif
 #include <stdio.h>
 #include <stdbool.h>
@@ -23,5 +25,5 @@
 #include "tw_version.h"
 
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 #include <windows.h>
 #endif
@@ -60,4 +62,11 @@
 #define printf(...) tt_printf(__VA_ARGS__)
 #define STDERR_LOG(...) tt_printf(__VA_ARGS__)
+#elif defined(_MSC_VER)
+void STDERR_LOG(const char* format, ...){
+	va_list args;
+	va_start(args, format);
+	vfprintf(stderr, format, args);
+	va_end(args);
+}
 #else
 #define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
@@ -72,5 +81,5 @@
 int startup(int argc, char** argv);
 
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 char* get_registry(const char* main, const char* sub) {
 	DWORD bufsize = 512;
@@ -511,4 +520,5 @@
 
 int main(int argc, char** argv) {
+	int st;
 	logfile = stderr;
 #ifdef SERVICE
@@ -667,5 +677,5 @@
 	SleepThread();
 #endif
-	int st = startup(argc, argv);
+	st = startup(argc, argv);
 	if(st != -1) {
 #ifdef _PSP
@@ -692,5 +702,6 @@
 int startup(int argc, char** argv) {
 	int i;
-#ifdef __MINGW32__
+	char* r;
+#if defined(__MINGW32__) || defined(_MSC_VER)
 	char* confpath = cm_strdup(PREFIX "/etc/tewi.conf");
 	char* regpath = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
@@ -770,8 +781,8 @@
 	}
 	sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
-	char* r = cm_strcat(tw_server, " running...");
+	r = cm_strcat(tw_server, " running...");
 	cm_force_log(r);
 	free(r);
-#ifndef __MINGW32__
+#if !defined(__MINGW32__) && !defined(_MSC_VER)
 	signal(SIGCHLD, SIG_IGN);
 	signal(SIGPIPE, SIG_IGN);
Index: trunk/Server/module.c
===================================================================
--- trunk/Server/module.c	(revision 209)
+++ trunk/Server/module.c	(revision 212)
@@ -11,6 +11,8 @@
 
 #include <string.h>
+#include <stdlib.h>
+#ifndef _MSC_VER
 #include <unistd.h>
-#include <stdlib.h>
+#endif
 
 extern struct tw_config config;
@@ -25,5 +27,5 @@
 #else
 
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 #include <windows.h>
 #else
@@ -33,7 +35,7 @@
 void* tw_module_load(const char* path) {
 	char* p = getcwd(NULL, 0);
+	void* lib;
 	chdir(config.server_root);
-	void* lib;
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 	lib = LoadLibraryA(path);
 #else
@@ -49,5 +51,5 @@
 
 void* tw_module_symbol(void* mod, const char* sym) {
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 	return GetProcAddress(mod, sym);
 #else
Index: trunk/Server/server.c
===================================================================
--- trunk/Server/server.c	(revision 209)
+++ trunk/Server/server.c	(revision 212)
@@ -16,5 +16,7 @@
 #include "tw_version.h"
 
+#ifndef _MSC_VER
 #include <unistd.h>
+#endif
 #include <string.h>
 #include <stdbool.h>
@@ -23,6 +25,6 @@
 #include <stdlib.h>
 #include <errno.h>
+#include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/types.h>
 #include <time.h>
 
@@ -31,5 +33,5 @@
 #include <cm_dir.h>
 
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 #ifndef NO_GETADDRINFO
 #include <ws2tcpip.h>
@@ -70,4 +72,8 @@
 #endif
 
+#ifndef S_ISDIR
+#define S_ISDIR(x) ((x) & _S_IFDIR)
+#endif
+
 extern struct tw_config config;
 extern char tw_server[];
@@ -78,5 +84,5 @@
 int sockets[MAX_PORTS];
 
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 const char* reserved_names[] = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
 #endif
@@ -105,5 +111,5 @@
 
 void close_socket(int sock) {
-#if defined(__MINGW32__)
+#if defined(__MINGW32__) || defined(_MSC_VER)
 	closesocket(sock);
 #else
@@ -114,5 +120,5 @@
 int tw_server_init(void) {
 	int i;
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 	WSADATA wsa;
 	WSAStartup(MAKEWORD(2, 0), &wsa);
@@ -122,4 +128,6 @@
 	sockcount = i;
 	for(i = 0; config.ports[i] != -1; i++) {
+		int yes = 1;
+		int no = 0;
 #ifdef NO_IPV6
 		int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -127,5 +135,5 @@
 		int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
 #endif
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 		if(sock == INVALID_SOCKET)
 #else
@@ -136,5 +144,4 @@
 			return 1;
 		}
-		int yes = 1;
 		if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
 			close_socket(sock);
@@ -150,5 +157,4 @@
 #endif
 #ifndef NO_IPV6
-		int no = 0;
 		if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
 			close_socket(sock);
@@ -223,4 +229,5 @@
 void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, char** headers, time_t mtime, time_t cmtime) {
 	char construct[512];
+	size_t incr;
 	if(mtime != 0 && cmtime != 0 && mtime <= cmtime) {
 		status = "304 Not Modified";
@@ -231,5 +238,9 @@
 		doc = NULL;
 	}
+#ifdef _MSC_VER
+	sprintf(construct, "%lu", (unsigned long)size);
+#else
 	sprintf(construct, "%llu", (unsigned long long)size);
+#endif
 	tw_write(ssl, sock, "HTTP/1.1 ", 9);
 	tw_write(ssl, sock, (char*)status, strlen(status));
@@ -256,6 +267,6 @@
 		}
 	}
-	int i;
 	if(headers != NULL) {
+		int i;
 		for(i = 0; headers[i] != NULL; i += 2) {
 			tw_write(ssl, sock, headers[i], strlen(headers[i]));
@@ -267,5 +278,5 @@
 	tw_write(ssl, sock, "\r\n", 2);
 	if(doc == NULL && f == NULL) return;
-	size_t incr = 0;
+	incr = 0;
 	while(1) {
 		if(f != NULL) {
@@ -308,4 +319,9 @@
 char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) {
 	char address[1024];
+	char* st;
+	char* st2;
+	char* buffer;
+	char* str;
+	int i;
 
 	if((vhost->hideport == -1 ? config.root.hideport : vhost->hideport) == 1) {
@@ -315,7 +331,5 @@
 	}
 
-	char* st = cm_strdup(tw_http_status(code));
-	char* st2;
-	int i;
+	st = cm_strdup(tw_http_status(code));
 	for(i = 0; st[i] != 0; i++) {
 		if(st[i] == ' ') {
@@ -324,6 +338,6 @@
 		}
 	}
-	char* buffer = malloc(4096);
-	char* str = cm_strcat3(ERROR_HTML);
+	buffer = malloc(4096);
+	str = cm_strcat3(ERROR_HTML);
 	sprintf(buffer, str, st, st2);
 	free(str);
@@ -341,6 +355,6 @@
 	int i;
 	char cbuf[2];
+	va_list va;
 	cbuf[1] = 0;
-	va_list va;
 	va_start(va, add);
 	for(i = 0; add[i] != 0; i++) {
@@ -367,6 +381,6 @@
 				int n = va_arg(va, int);
 				char* h = malloc(512);
+				char* tmp = *str;
 				sprintf(h, "%d", n);
-				char* tmp = *str;
 				*str = cm_strcat(tmp, h);
 				free(tmp);
@@ -388,7 +402,7 @@
 char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) {
 	char* mime = "application/octet-stream";
-	if(ext == NULL) return mime;
 	bool set = false;
 	int i;
+	if(ext == NULL) return mime;
 	for(i = 0; i < vhost_entry->mime_count; i++) {
 		if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
@@ -409,7 +423,7 @@
 char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
 	char* icon = "";
-	if(mime == NULL) return "";
 	bool set = false;
 	int i;
+	if(mime == NULL) return "";
 	for(i = 0; i < vhost_entry->icon_count; i++) {
 		if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
@@ -435,5 +449,5 @@
 };
 
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 unsigned int WINAPI tw_server_pass(void* ptr) {
 #elif defined(__HAIKU__)
@@ -442,18 +456,16 @@
 int tw_server_pass(void* ptr) {
 #endif
-#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__)
+#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER)
+#define FREE_PTR
 	int sock = ((struct pass_entry*)ptr)->sock;
 	bool ssl = ((struct pass_entry*)ptr)->ssl;
 	int port = ((struct pass_entry*)ptr)->port;
 	SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
-	free(ptr);
 #else
 	void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
 #endif
-	char* name = config.hostname;
-
+	SSL* s = NULL;
 #ifndef NO_SSL
 	SSL_CTX* ctx = NULL;
-	SSL* s = NULL;
 	bool sslworks = false;
 	if(ssl) {
@@ -464,30 +476,43 @@
 		sslworks = true;
 	}
-#else
-		void* s = NULL;
-#endif
-
+#endif
+	char* name = config.hostname;
 	char address[513];
-	address[0] = 0;
+	int ret;
+	struct tw_http_request req;
+	struct tw_http_response res;
+	struct tw_tool tools;
 #ifndef NO_GETADDRINFO
 	struct sockaddr* sa = (struct sockaddr*)&addr;
 	getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST);
 #endif
-
-	struct tw_http_request req;
-	struct tw_http_response res;
-	struct tw_tool tools;
+	address[0] = 0;
+#ifdef FREE_PTR
+	free(ptr);
+#endif
+
 	res._processed = false;
 	tw_init_tools(&tools);
-	int ret = tw_http_parse(s, sock, &req);
+	ret = tw_http_parse(s, sock, &req);
 	if(ret == 0) {
 		char date[513];
 		time_t t = time(NULL);
 		struct tm* tm = localtime(&t);
+		char* useragent = cm_strdup("");
+		int i;
+		char* tmp;
+		char* tmp2;
+		char* tmp3;
+		char* tmp4;
+		char* tmp5;
+		char* log;
+		char* vhost;
+		time_t cmtime;
+		bool rej;
+		char* host;
+		int port;
+		struct tw_config_entry* vhost_entry;
 		strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm);
 
-		char* useragent = cm_strdup("");
-
-		int i;
 		for(i = 0; req.headers[i] != NULL; i += 2) {
 			if(cm_strcaseequ(req.headers[i], "User-Agent")) {
@@ -497,10 +522,10 @@
 		}
 
-		char* tmp = cm_strcat3(address, " - [", date);
-		char* tmp2 = cm_strcat3(tmp, "] \"", req.method);
-		char* tmp3 = cm_strcat3(tmp2, " ", req.path);
-		char* tmp4 = cm_strcat3(tmp3, " ", req.version);
-		char* tmp5 = cm_strcat3(tmp4, "\" \"", useragent);
-		char* log = cm_strcat(tmp5, "\"");
+		tmp = cm_strcat3(address, " - [", date);
+		tmp2 = cm_strcat3(tmp, "] \"", req.method);
+		tmp3 = cm_strcat3(tmp2, " ", req.path);
+		tmp4 = cm_strcat3(tmp3, " ", req.version);
+		tmp5 = cm_strcat3(tmp4, "\" \"", useragent);
+		log = cm_strcat(tmp5, "\"");
 		free(tmp);
 		free(tmp2);
@@ -512,6 +537,6 @@
 		free(log);
 
-		char* vhost = cm_strdup(config.hostname);
-		time_t cmtime = 0;
+		vhost = cm_strdup(config.hostname);
+		cmtime = 0;
 		if(req.headers != NULL) {
 			for(i = 0; req.headers[i] != NULL; i += 2) {
@@ -521,8 +546,10 @@
 				} else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
 					struct tm tm;
+					time_t t;
+					struct tm* btm;
 					strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
-#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__)
-					time_t t = 0;
-					struct tm* btm = localtime(&t);
+#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER)
+					t = 0;
+					btm = localtime(&t);
 					cmtime = mktime(&tm);
 					cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
@@ -533,8 +560,8 @@
 			}
 		}
-		bool rej = false;
+		rej = false;
 		cm_log("Server", "Host is %s", vhost);
-		int port = s == NULL ? 80 : 443;
-		char* host = cm_strdup(vhost);
+		port = s == NULL ? 80 : 443;
+		host = cm_strdup(vhost);
 		for(i = 0; vhost[i] != 0; i++) {
 			if(vhost[i] == ':') {
@@ -546,5 +573,5 @@
 		name = host;
 		cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
-		struct tw_config_entry* vhost_entry = tw_vhost_match(host, port);
+		vhost_entry = tw_vhost_match(host, port);
 #ifdef HAS_CHROOT
 		char* chrootpath = vhost_entry->chroot_path != NULL ? vhost_entry->chroot_path : config.root.chroot_path;
@@ -582,9 +609,12 @@
 		}
 		if(!res._processed) {
+			char* path;
+			char* rpath;
+			struct stat st;
 			cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
-			char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
+			path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
 			cm_log("Server", "Filesystem path is %s", path);
-#ifdef __MINGW32__
-			char* rpath = cm_strdup(path);
+#if defined(__MINGW32__) || defined(_MSC_VER)
+			rpath = cm_strdup(path);
 			for(i = strlen(rpath) - 1; i >= 0; i--) {
 				if(rpath[i] == '/') {
@@ -612,5 +642,4 @@
 			free(rpath);
 #endif
-			struct stat st;
 			if(!rej && stat(path, &st) == 0) {
 				if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
@@ -618,6 +647,6 @@
 				} else if(S_ISDIR(st.st_mode)) {
 					if(req.path[strlen(req.path) - 1] != '/') {
+						char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
 						cm_log("Server", "Accessing directory without the slash at the end");
-						char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL};
 						_tw_process_page(s, sock, tw_http_status(301), NULL, NULL, NULL, 0, headers, 0, 0);
 						free(headers[1]);
@@ -632,4 +661,6 @@
 								char* ext = NULL;
 								int j;
+								struct stat st;
+								char* mime;
 								for(j = strlen(p) - 1; j >= 0; j--) {
 									if(p[j] == '.') {
@@ -640,7 +671,6 @@
 									}
 								}
-								struct stat st;
 								stat(p, &st);
-								char* mime = tw_get_mime(ext, vhost_entry);
+								mime = tw_get_mime(ext, vhost_entry);
 								tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
 								fclose(f);
@@ -654,6 +684,11 @@
 						if(!found) {
 							char* str = malloc(1);
+							char** items;
+							int readme;
+							char** readmes;
+							int readme_count;
+							int hp;
 							str[0] = 0;
-							char** items = cm_scandir(path);
+							items = cm_scandir(path);
 							addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n");
 							addstring(&str, "<html>\n");
@@ -673,7 +708,7 @@
 							addstring(&str, "				<th>Size</th>\n");
 							addstring(&str, "			</tr>\n");
-							int readme = -1;
-							char** readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
-							int readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
+							readme = -1;
+							readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
+							readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
 							if(items != NULL) {
 								int phase = 0;
@@ -681,7 +716,12 @@
 								for(i = 0; items[i] != NULL; i++) {
 									int j;
+									char* ext;
+									char* itm;
+									char* icon;
 									char* fpth = cm_strcat3(path, "/", items[i]);
 									struct stat s;
 									char size[512];
+									char* showmime;
+									char* mime;
 									size[0] = 0;
 									stat(fpth, &s);
@@ -705,13 +745,13 @@
 										}
 									}
-									if(s.st_size < 1024ULL) {
+									if(s.st_size < NUM1024) {
 										sprintf(size, "%d", (int)s.st_size);
-									} else if(s.st_size < 1024ULL * 1024) {
+									} else if(s.st_size < NUM1024 * 1024) {
 										sprintf(size, "%.1fK", (double)s.st_size / 1024);
-									} else if(s.st_size < 1024ULL * 1024 * 1024) {
+									} else if(s.st_size < NUM1024 * 1024 * 1024) {
 										sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
-									} else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) {
+									} else if(s.st_size < NUM1024 * 1024 * 1024 * 1024) {
 										sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
-									} else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
+									} else if(s.st_size < NUM1024 * 1024 * 1024 * 1024 * 1024) {
 										sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
 									}
@@ -719,5 +759,5 @@
 									free(fpth);
 
-									char* ext = NULL;
+									ext = NULL;
 									for(j = strlen(items[i]) - 1; j >= 0; j--) {
 										if(items[i][j] == '.') {
@@ -728,6 +768,6 @@
 										}
 									}
-									char* showmime = "";
-									char* mime = tw_get_mime(ext, vhost_entry);
+									showmime = "";
+									mime = tw_get_mime(ext, vhost_entry);
 									if(strcmp(items[i], "../") == 0) {
 										mime = "misc/parent";
@@ -739,7 +779,7 @@
 										showmime = mime;
 									}
-									char* icon = tw_get_icon(mime, vhost_entry);
+									icon = tw_get_icon(mime, vhost_entry);
 									if(ext != NULL) free(ext);
-									char* itm = cm_strdup(items[i]);
+									itm = cm_strdup(items[i]);
 									if(strlen(itm) >= 32) {
 										if(itm[strlen(itm) - 1] == '/') {
@@ -771,9 +811,11 @@
 							addstring(&str, "		</table>\n");
 							if(readme != -1) {
+								struct stat s;
+								FILE* fr;
+								char* fpth;
 								addstring(&str, "<hr>\n");
-								char* fpth = cm_strcat3(path, "/", readmes[readme]);
-								struct stat s;
+								fpth = cm_strcat3(path, "/", readmes[readme]);
 								stat(fpth, &s);
-								FILE* fr = fopen(fpth, "r");
+								fr = fopen(fpth, "r");
 								if(fr != NULL) {
 									char* rmbuf = malloc(s.st_size + 1);
@@ -787,5 +829,5 @@
 							}
 							addstring(&str, "		<hr>\n");
-							int hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport;
+							hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport;
 							if(hp == 0) {
 								addstring(&str, "		<address>%s Server at %s Port %d</address>\n", tw_server, name, port);
@@ -801,4 +843,6 @@
 				} else {
 					char* ext = NULL;
+					char* mime;
+					FILE* f;
 					for(i = strlen(req.path) - 1; i >= 0; i--) {
 						if(req.path[i] == '.') {
@@ -809,7 +853,7 @@
 						}
 					}
-					char* mime = tw_get_mime(ext, vhost_entry);
+					mime = tw_get_mime(ext, vhost_entry);
 					if(ext != NULL) free(ext);
-					FILE* f = fopen(path, "rb");
+					f = fopen(path, "rb");
 					if(f == NULL) {
 						tw_http_error(s, sock, 403, name, port, vhost_entry);
@@ -841,6 +885,6 @@
 #endif
 	close_socket(sock);
-#ifdef __MINGW32__
-	_endthreadex(0);
+#if defined(__MINGW32__) || defined(_MSC_VER)
+	_endthread(0);
 #elif defined(__HAIKU__)
 		exit_thread(0);
@@ -854,5 +898,5 @@
 #endif
 
-#if defined(__MINGW32__) || defined(__HAIKU__)
+#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER)
 struct thread_entry {
 #ifdef __HAIKU__
@@ -869,5 +913,9 @@
 void tw_server_loop(void) {
 	int i;
-#if defined(__MINGW32__) || defined(__HAIKU__)
+#ifndef USE_POLL
+		fd_set fdset;
+		struct timeval tv;
+#endif
+#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER)
 	struct thread_entry threads[2048];
 	for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
@@ -881,11 +929,9 @@
 		pollfds[i].events = POLLIN | POLLPRI;
 	}
-#else
-		fd_set fdset;
-		struct timeval tv;
 #endif
 	while(running) {
+		int ret;
 #ifdef USE_POLL
-		int ret = poll(pollfds, sockcount, 1000);
+		ret = poll(pollfds, sockcount, 1000);
 #else
 			FD_ZERO(&fdset);
@@ -896,11 +942,11 @@
 			tv.tv_usec = 0;
 #ifdef __HAIKU__
-			int ret = select(32, &fdset, NULL, NULL, &tv);
-#else
-			int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
+			ret = select(32, &fdset, NULL, NULL, &tv);
+#else
+			ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
 #endif
 #endif
 		if(ret == -1) {
-#ifndef __MINGW32__
+#if !defined(__MINGW32__) && !defined(_MSC_VER)
 			cm_log("Server", "Select failure: %s", strerror(errno));
 #endif
@@ -926,35 +972,22 @@
 					socklen_t clen = sizeof(claddr);
 					int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
+#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER)
+					int j;
+					struct pass_entry* e = malloc(sizeof(*e));
 					cm_log("Server", "New connection accepted");
-#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__)
-					struct pass_entry* e = malloc(sizeof(*e));
 					e->sock = sock;
-					e->ssl = config.ports[i] & (1ULL << 32);
+#ifdef _MSC_VER
+					e->ssl = config.ports[i] & (1UL << 31);
+#else
+					e->ssl = config.ports[i] & (1ULL << 31);
+#endif
 					e->port = config.ports[i];
 					e->addr = claddr;
 #endif
-#ifdef __MINGW32__
-					int j;
-					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
-						if(threads[j].used) {
-							DWORD ex;
-							GetExitCodeThread(threads[j].handle, &ex);
-							if(ex != STILL_ACTIVE) {
-								CloseHandle(threads[j].handle);
-								threads[j].used = false;
-							}
-						}
-					}
-					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
-						if(!threads[j].used) {
-							threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
-							threads[j].used = true;
-							break;
-						}
-					}
+#if defined(__MINGW32__) || defined(_MSC_VER)
+					_beginthread(tw_server_pass, 0, e);
 #elif defined(_PSP) || defined(__PPU__)
 						tw_server_pass(e);
 #elif defined(__HAIKU__)
-					int j;
 					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
 						if(threads[j].used) {
@@ -983,5 +1016,5 @@
 						int j;
 						for(j = 0; j < sockcount; j++) close_socket(sockets[j]);
-						tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
+						tw_server_pass(sock, config.ports[i] & (1ULL << 31), config.ports[i], claddr);
 						_exit(0);
 					} else {
Index: trunk/Server/strptime.c
===================================================================
--- trunk/Server/strptime.c	(revision 209)
+++ trunk/Server/strptime.c	(revision 212)
@@ -36,5 +36,5 @@
 //__RCSID("$NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $");
 
-#if defined(__MINGW32__)
+#if defined(__MINGW32__) || defined(_MSC_VER)
 
 #include <ctype.h>
@@ -292,8 +292,9 @@
         case 'D':	/* The date as "%y/%m/%d". */
         {
+	    int year;
             new_fmt = HERE_D_FMT;
             LEGAL_ALT(0);
             state |= S_MON | S_MDAY | S_YEAR;
-            const int year = split_year ? tm->tm_year : 0;
+            year = split_year ? tm->tm_year : 0;
 
             bp = (const unsigned char *)strptime((const char *)bp,
@@ -399,10 +400,18 @@
 
 #ifndef TIME_MAX
+#ifdef _MSC_VER
+#define TIME_MAX	INT32_MAX
+#else
 #define TIME_MAX	INT64_MAX
+#endif
 #endif
         case 's':	/* seconds since the epoch */
             {
                 time_t sse = 0;
+#ifdef _MSC_VER
+                uint32_t rulim = TIME_MAX;
+#else
                 uint64_t rulim = TIME_MAX;
+#endif
 
                 if (*bp < '0' || *bp > '9') {
@@ -417,11 +426,18 @@
                 } while ((sse * 10 <= TIME_MAX) &&
                      rulim && *bp >= '0' && *bp <= '9');
-
+#ifdef _MSC_VER
+                if (sse < 0 || (uint32_t)sse > TIME_MAX) {
+#else
                 if (sse < 0 || (uint64_t)sse > TIME_MAX) {
+#endif
                     bp = NULL;
                     continue;
                 }
 #ifdef _WIN32
+#ifdef _MSC_VER
+		if (1)
+#else
                 if (localtime_s(tm, &sse) == 0)
+#endif
 #else
                 if (localtime_r(&sse, tm))
Index: trunk/Server/tw_config.h
===================================================================
--- trunk/Server/tw_config.h	(revision 209)
+++ trunk/Server/tw_config.h	(revision 212)
@@ -15,5 +15,5 @@
 #include <stdbool.h>
 
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_MSC_VER)
 #include <winsock2.h>
 #define NO_IPV6
@@ -42,4 +42,10 @@
 #define MAX_INDEX 1024
 #define MAX_README 8
+
+#ifdef _MSC_VER
+#define NUM1024 1024UL
+#else
+#define NUM1024 1024ULL
+#endif
 
 enum TW_DIR_TYPE {
@@ -89,5 +95,9 @@
 
 struct tw_config {
+#ifdef _MSC_VER
+	uint32_t ports[MAX_PORTS + 1];
+#else
 	uint64_t ports[MAX_PORTS + 1]; /* If port & (1 << 32) is non-zero, it is SSL */
+#endif
 	char hostname[1025];
 	char* defined[1025];
Index: trunk/Server/version.c
===================================================================
--- trunk/Server/version.c	(revision 209)
+++ trunk/Server/version.c	(revision 212)
@@ -14,5 +14,5 @@
 #elif defined(__linux__)
     "Linux"
-#elif defined(__MINGW32__)
+#elif defined(__MINGW32__) || defined(_MSC_VER)
     "Windows"
 #elif defined(__HAIKU__)
