diff --git a/ChangeLog b/ChangeLog
index 6206f02b0b34df9cb59e1122af69f5b0642bf3d9..3e90921ef593d32fe4910866410f7ef228acfb97 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2005-11-01  Kevin P. Fleming  <kpfleming@digium.com>
 
+	* many files: more Cygwin build system support (issue #4678)
+
 	* apps/app_parkandannounce.c (parkandannounce_exec): supply parent channel to ast_request_and_dial so channel variables can be inherited (issue #5564)
 	* include/asterisk/channel.h: add parent_channel field
 	* channel.c (__ast_request_and_dial): use parent_channel field to inherit variables into new channel
diff --git a/Makefile b/Makefile
index 4170b08281daef3acdea20062130e9c7bf83cfed..49511b5585937dcaf453fd66a38c36820f2e137d 100755
--- a/Makefile
+++ b/Makefile
@@ -216,6 +216,7 @@ endif
 INCLUDE+=-Iinclude -I../include
 ASTCFLAGS+=-pipe  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(DEBUG) $(INCLUDE) -D_REENTRANT -D_GNU_SOURCE #-DMAKE_VALGRIND_HAPPY
 ASTCFLAGS+=$(OPTIMIZE)
+ASTOBJ=-o asterisk
 
 ifeq ($(findstring BSD,${OSARCH}),BSD)
   PROC=$(shell uname -m)
@@ -263,6 +264,15 @@ ifeq (${OSARCH},SunOS)
   INCLUDE+=-Iinclude/solaris-compat -I$(CROSS_COMPILE_TARGET)/usr/local/ssl/include
 endif
 
+ifeq ($(findstring CYGWIN,${OSARCH}),CYGWIN)
+CYGLOADER=cygwin_a
+OSARCH=CYGWIN
+ASTOBJ=-shared -o asterisk.dll -Wl,--out-implib=libasterisk.dll.a -Wl,--export-all-symbols
+ASTLINK=
+LIBS+=-lpthread -lncurses -lm -lresolv
+ASTSBINDIR=${MODULES_DIR}
+endif
+
 ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/linux/zaptel.h)$(wildcard $(CROSS_COMPILE_TARGET)/usr/local/include/zaptel.h)$(wildcard $(CROSS_COMPILE_TARGET)/usr/pkg/include/zaptel.h),)
   ASTCFLAGS+=-DZAPTEL_OPTIMIZATIONS
 endif
@@ -471,8 +481,11 @@ stdtime/libtime.a: FORCE
 		exit 1; \
 	fi
 
-asterisk: editline/libedit.a db1-ast/libdb1.a stdtime/libtime.a $(OBJS)
-	$(CC) $(DEBUG) -o asterisk $(ASTLINK) $(OBJS) $(LIBEDIT) db1-ast/libdb1.a stdtime/libtime.a $(LIBS)
+cygwin_a:
+	$(MAKE) -C cygwin all
+
+asterisk: ${CYGLOADER} editline/libedit.a db1-ast/libdb1.a stdtime/libtime.a $(OBJS)
+	$(CC) $(DEBUG) ${ASTOBJ} $(ASTLINK) $(OBJS) $(LIBEDIT) db1-ast/libdb1.a stdtime/libtime.a $(LIBS)
 
 muted: muted.o
 	$(CC) $(AUDIO_LIBS) -o muted muted.o
@@ -595,7 +608,9 @@ bininstall: all
 	mkdir -p $(DESTDIR)$(ASTSPOOLDIR)/system
 	mkdir -p $(DESTDIR)$(ASTSPOOLDIR)/tmp
 	mkdir -p $(DESTDIR)$(ASTSPOOLDIR)/meetme
-	$(INSTALL) -m 755 asterisk $(DESTDIR)$(ASTSBINDIR)/
+	if [ -f asterisk ]; then $(INSTALL) -m 755 asterisk $(DESTDIR)$(ASTSBINDIR)/; fi
+	if [ -f cygwin/asterisk.exe ]; then $(INSTALL) -m 755 cygwin/asterisk.exe $(DESTDIR)$(ASTSBINDIR)/; fi
+	if [ -f asterisk.dll ]; then $(INSTALL) -m 755 asterisk.dll $(DESTDIR)$(ASTSBINDIR)/; fi
 	ln -sf asterisk $(DESTDIR)$(ASTSBINDIR)/rasterisk
 	$(INSTALL) -m 755 contrib/scripts/astgenkey $(DESTDIR)$(ASTSBINDIR)/
 	$(INSTALL) -m 755 contrib/scripts/autosupport $(DESTDIR)$(ASTSBINDIR)/	
diff --git a/acl.c b/acl.c
index 7fd13ccf5d7432843f99a145d417e1410670f820..326cdb66d61c4e640f4bde27dd410b8656e33aa6 100755
--- a/acl.c
+++ b/acl.c
@@ -68,6 +68,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/utils.h"
 #include "asterisk/lock.h"
 #include "asterisk/srv.h"
+#include "asterisk/compat.h"
 
 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
 AST_MUTEX_DEFINE_STATIC(routeseq_lock);
diff --git a/apps/Makefile b/apps/Makefile
index f7e309e3fc76b4ebc865a384d0252dbbe4fc465f..4c276c25aa2444163939163689233b4baf4f87c6 100755
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -22,7 +22,7 @@ APPS=app_dial.so app_playback.so app_voicemail.so app_directory.so app_mp3.so\
      app_enumlookup.so app_transfer.so app_setcidnum.so app_cdr.so \
      app_hasnewvoicemail.so app_sayunixtime.so app_cut.so app_read.so \
      app_setcdruserfield.so app_random.so app_ices.so app_eval.so \
-     app_nbscat.so app_sendtext.so app_exec.so app_sms.so \
+     app_nbscat.so app_sendtext.so app_exec.so \
      app_groupcount.so app_txtcidname.so app_controlplayback.so \
      app_talkdetect.so app_alarmreceiver.so app_userevent.so app_verbose.so \
      app_test.so app_forkcdr.so app_math.so app_realtime.so \
@@ -63,7 +63,13 @@ ifneq ($(shell if [[ 0x`$(CROSS_COMPILE_BIN)curl-config --vernum` -ge 0x70907 ]]
   endif 
 endif
 
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -L../res -lasterisk.dll -lres_features.so -lres_adsi.so -lres_monitor.so
+else
 CFLAGS+=-fPIC
+APPS+=app_sms.so
+endif
 
 #
 # If you have UnixODBC you can use ODBC voicemail
@@ -81,10 +87,10 @@ clean:
 	rm -f *.so *.o look .depend
 
 %.so : %.o
-	$(CC) $(SOLINK) -o $@ $<
-
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
+	
 app_rpt.so : app_rpt.o
-	$(CC) $(SOLINK) -o $@ $< -ltonezone
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -ltonezone
 
 install: all
 	for x in $(APPS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
@@ -92,16 +98,16 @@ install: all
 	rm -f $(DESTDIR)$(MODULES_DIR)/app_qcall.so
 
 app_curl.so: app_curl.o
-	$(CC) $(SOLINK) -o $@ $< $(CURLLIBS)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(CURLLIBS)
 
 app_sql_postgres.o: app_sql_postgres.c
 	$(CC) -pipe -I/usr/local/pgsql/include $(CFLAGS) -c -o app_sql_postgres.o app_sql_postgres.c
 
 app_sql_postgres.so: app_sql_postgres.o
-	$(CC) $(SOLINK) -o $@ $< -L/usr/local/pgsql/lib -lpq
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -L/usr/local/pgsql/lib -lpq
 
 app_sql_odbc.so: app_sql_odbc.o
-	$(CC) $(SOLINK) -o $@ $< -lodbc
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lodbc
 
 look:	look.c
 	$(CC) -pipe -O6 -g look.c -o look -lncurses
diff --git a/ast_expr2.c b/ast_expr2.c
index e68bd84a1b1fac309aec0cb3f319cfd5d799b4d8..39928d5f7be53045f09c7dcad81d4c97c34ef909 100755
--- a/ast_expr2.c
+++ b/ast_expr2.c
@@ -132,7 +132,7 @@
 #include <string.h>
 #include <locale.h>
 #include <ctype.h>
-#ifndef SOLARIS
+#if !defined(SOLARIS) && !defined(__CYGWIN__)
 #include <err.h>
 #else
 #define quad_t int64_t
diff --git a/ast_expr2.fl b/ast_expr2.fl
index 39df6013b4b9fda9a5b146ad60ef6db79da443c2..7a54ed36b4ce163ec52e4948cc5d846131608ca7 100755
--- a/ast_expr2.fl
+++ b/ast_expr2.fl
@@ -5,7 +5,7 @@
 #include <string.h>
 #include <locale.h>
 #include <ctype.h>
-#ifndef SOLARIS
+#if !defined(SOLARIS) && !defined(__CYGWIN__)
 #include <err.h>
 #else
 #define quad_t int64_t
diff --git a/ast_expr2.y b/ast_expr2.y
index 9aab0f164648aa373dcac02e2e2d408a623d3a39..15da7ecb8561e9989396f804da9de48676ca39ab 100755
--- a/ast_expr2.y
+++ b/ast_expr2.y
@@ -18,7 +18,7 @@
 #include <string.h>
 #include <locale.h>
 #include <ctype.h>
-#ifndef SOLARIS
+#if !defined(SOLARIS) && !defined(__CYGWIN__)
 #include <err.h>
 #else
 #define quad_t int64_t
diff --git a/ast_expr2f.c b/ast_expr2f.c
index b3e88f1e6a42ff5f91a0910c238bf6d4be3ad324..defaa100ca9c8c6dd82d65c5eaec7a619cf299fb 100755
--- a/ast_expr2f.c
+++ b/ast_expr2f.c
@@ -956,7 +956,7 @@ static yyconst yy_state_type yy_NUL_trans[33] =
 #include <string.h>
 #include <locale.h>
 #include <ctype.h>
-#ifndef SOLARIS
+#if !defined(SOLARIS) && !defined(__CYGWIN__)
 #include <err.h>
 #else
 #define quad_t int64_t
diff --git a/asterisk.c b/asterisk.c
index 4d796c4c11780a14c2ff88246cc3520a178c5950..785b64a915956aa43477e5df95e951be440c587c 100755
--- a/asterisk.c
+++ b/asterisk.c
@@ -2048,6 +2048,8 @@ int main(int argc, char *argv[])
 		ast_verbose("[ Reading Master Configuration ]");
 	ast_readconfig();
 
+#ifndef __CYGWIN__
+
 	if (!is_child_of_nonroot && ast_set_priority(option_highpriority)) {
 		exit(1);
 	}
@@ -2083,6 +2085,8 @@ int main(int argc, char *argv[])
 			ast_verbose("Running as user '%s'\n", runuser);
 	}
 
+#endif /* __CYGWIN__ */
+
 	term_init();
 	printf(term_end());
 	fflush(stdout);
diff --git a/cdr/Makefile b/cdr/Makefile
index e3db4ba228809dd0e0994714c3678031453342f3..3f6a7602e2f850a29224cd550636294c3b4367d3 100755
--- a/cdr/Makefile
+++ b/cdr/Makefile
@@ -13,7 +13,12 @@
 
 MODS=cdr_csv.so cdr_manager.so cdr_custom.so
 
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -lasterisk.dll
+else
 CFLAGS+=-fPIC
+endif
 
 ifeq ($(findstring BSD,${OSARCH}),BSD)
   CFLAGS+=-I$(CROSS_COMPILE_TARGET)/usr/local/include -L$(CROSS_COMPILE_TARGET)/usr/local/lib
@@ -110,23 +115,23 @@ clean:
 	rm -f *.so *.o .depend
 
 %.so : %.o
-	$(CC) $(SOLINK) -o $@ $<
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
 
 ifneq ($(wildcard .depend),)
   include .depend
 endif
 
 cdr_odbc.so: cdr_odbc.o
-	$(CC) $(SOLINK) -o $@ $< -lodbc $(MLFLAGS)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lodbc $(MLFLAGS)
 
 cdr_tds.so: cdr_tds.o
-	$(CC) $(SOLINK) -o $@ $< -ltds $(MLFLAGS)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -ltds $(MLFLAGS)
 
 cdr_pgsql.so: cdr_pgsql.o
-	$(CC) $(SOLINK) -o $@ $< -lpq -lz $(MLFLAGS)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lpq -lz $(MLFLAGS)
 
 cdr_sqlite.so: cdr_sqlite.o
-	$(CC) $(SOLINK) -o $@ $< -lsqlite $(MLFLAGS)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lsqlite $(MLFLAGS)
 
 depend: .depend
 
diff --git a/channels/Makefile b/channels/Makefile
index 068cbe14b82eb5503034aec5af1869f755170033..55f50b30f37c96a887b009e6c677cb61d519f5a2 100755
--- a/channels/Makefile
+++ b/channels/Makefile
@@ -14,11 +14,11 @@
 # the GNU General Public License
 #
 
-CHANNEL_LIBS=chan_modem.so chan_sip.so \
-	     chan_modem_aopen.so \
-             chan_modem_bestdata.so \
-             chan_agent.so chan_mgcp.so chan_iax2.so \
-	     chan_local.so chan_skinny.so chan_features.so
+CHANNEL_LIBS=chan_sip.so chan_agent.so chan_mgcp.so chan_iax2.so chan_local.so chan_skinny.so chan_features.so
+
+ifneq (${OSARCH},CYGWIN)
+CHANNEL_LIBS+=chan_modem.so chan_modem_aopen.so chan_modem_bestdata.so
+endif
 
 ifeq ($(findstring BSD,${OSARCH}),BSD)
  CFLAGS+=-I$(CROSS_COMPILE_TARGET)/usr/local/include -L$(CROSS_COMPILE_TARGET)/usr/local/lib
@@ -35,6 +35,12 @@ ifeq (${OSARCH},Linux)
   CHANH323LIB=-ldl
 endif
 
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -L../res -lasterisk.dll -lres_features.so
+CYG_CHAN_AGENT=-lres_monitor.so
+endif
+
 ifeq ($(PROC),sparc64)
   PROC=ultrasparc
   CFLAGS += -mtune=$(PROC) -O3 -pipe -fomit-frame-pointer -mcpu=v8
@@ -53,7 +59,9 @@ endif
 
 ifneq (${OSARCH},Darwin)
   ifneq (${OSARCH},SunOS)
-    CHANNEL_LIBS+=chan_oss.so chan_modem_i4l.so 
+    ifneq (${OSARCH},CYGWIN)
+      CHANNEL_LIBS+=chan_oss.so chan_modem_i4l.so 
+    endif
   endif
 endif
 
@@ -113,7 +121,10 @@ ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/vpbapi.h),)
 endif
 
 CFLAGS+=-DCRYPTO
+
+ifneq ($(OSARCH),CYGWIN)
 CFLAGS+=-fPIC
+endif
 
 CFLAGS+=#-DVOFRDUMPER
 
@@ -140,7 +151,7 @@ clean:
 	rm -f busy.h ringtone.h gentone gentone-ulaw
 
 %.so : %.o
-	$(CC) $(SOLINK) -o $@ $<
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} ${LIBS}
 
 ifneq ($(wildcard .depend),)
   include .depend
@@ -180,7 +191,7 @@ chan_oss.so: chan_oss.o
 endif
 
 chan_iax2.so: chan_iax2.o iax2-parser.o iax2-provision.o
-	$(CC) $(SOLINK) -o $@ chan_iax2.o iax2-parser.o iax2-provision.o
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} chan_iax2.o iax2-parser.o iax2-provision.o ${CYGSOLIB}
 
 chan_zap.o: chan_zap.c
 	$(CC) -c $(CFLAGS) -o chan_zap.o chan_zap.c
@@ -189,7 +200,10 @@ chan_zap.so: chan_zap.o
 	$(CC) $(SOLINK) -o $@ $<  $(ZAPPRI) $(ZAPR2) -ltonezone
 
 chan_sip.so: chan_sip.o
-	$(CC) $(SOLINK) -o $@ chan_sip.o
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} chan_sip.o ${CYGSOLIB}
+
+chan_agent.so: chan_agent.o
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} chan_agent.o ${CYGSOLIB} ${CYG_CHAN_AGENT}
 
 chan_alsa.o: $(ALSA_SRC)
 
diff --git a/codecs/Makefile b/codecs/Makefile
index 602215886d08ed10bce92d910ac9f644d5f2affb..edf0292198de367a5137ef1a482d75c4842a9758 100755
--- a/codecs/Makefile
+++ b/codecs/Makefile
@@ -11,6 +11,11 @@
 # the GNU General Public License
 #
 
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -lasterisk.dll
+endif
+
 ifneq ($(wildcard g723.1/coder.c),)
   MODG723=codec_g723_1.so
   LIBG723=g723.1/libg723.a
@@ -92,28 +97,28 @@ $(LIBILBC):
 	$(MAKE) -C ilbc all
 
 $(MODILBC): codec_ilbc.o $(LIBILBC)
-	$(CC) $(SOLINK) -o $@ $< $(LIBILBC)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(LIBILBC)
 
 codec_g723_1.so : codec_g723_1.o $(LIBG723)
-	$(CC) $(SOLINK) -o $@ $< $(LIBG723)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(LIBG723)
 
 codec_g723_1b.o : codec_g723_1.c
 	$(CC) -c -o $@ $(CFLAGS) -DANNEX_B -Dsingle $<
 
 codec_g723_1b.so : codec_g723_1b.o $(LIBG723B)
-	$(CC) $(SOLINK) -o $@ $< $(LIBG723B) -lm
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(LIBG723B) -lm
 
 codec_gsm.so: codec_gsm.o $(LIBGSMT) 
-	$(CC) $(SOLINK) -o $@ $< $(LIBGSM)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(LIBGSM)
 
 $(MODSPEEX): codec_speex.o
-	$(CC) $(SOLINK) -o $@ $< $(LIBSPEEX)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(LIBSPEEX)
 
 codec_lpc10.so: codec_lpc10.o $(LIBLPC10)
-	$(CC) $(SOLINK) -o $@ $< $(LIBLPC10) -lm
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(LIBLPC10) -lm
 
 %.so : %.o
-	$(CC) $(SOLINK) -o $@ $<
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
 
 ifneq ($(wildcard .depend),)
   include .depend
diff --git a/codecs/gsm/Makefile b/codecs/gsm/Makefile
index 76f9e89ae78c0007c574e3206c8b7c3c1b0bec3b..a2b5dcf1e6e9eb272ff27037b19c4a4b653a9907 100755
--- a/codecs/gsm/Makefile
+++ b/codecs/gsm/Makefile
@@ -36,6 +36,7 @@ WAV49	= -DWAV49
 ######### which support MMX instructions.  This should be newer pentiums,
 ######### ppro's, etc, as well as the AMD K6 and K7.  The compile will
 ######### probably require gcc. 
+PROC=$(shell uname -m)
 
 ifneq (${OSARCH},Darwin)
 ifneq (${OSARCH},SunOS)
@@ -90,7 +91,11 @@ PG =
 # CCFLAGS 	= -c -O
 
 CC		?= gcc
-CCFLAGS 	+= -c -DNeedFunctionPrototypes=1 -funroll-loops -fPIC $(OPTIMIZE)
+CCFLAGS 	+= -c -DNeedFunctionPrototypes=1 -funroll-loops $(OPTIMIZE)
+ifneq ($(findstring CYGWIN,${OSARCH}),CYGWIN)
+CCFLAGS   += -fPIC
+endif
+
 
 LD 		= $(CC)
 
diff --git a/config.c b/config.c
index f279b1640a9068dd237cddc936b2d53f82541dc4..beb2f4df3c7309431e0c899f0eaa54a056678054 100755
--- a/config.c
+++ b/config.c
@@ -33,7 +33,7 @@
 #include <sys/stat.h>
 #define AST_INCLUDE_GLOB 1
 #ifdef AST_INCLUDE_GLOB
-#ifdef __Darwin__
+#if defined(__Darwin__) || defined(__CYGWIN__)
 #define GLOB_ABORTED GLOB_ABEND
 #endif
 # include <glob.h>
@@ -693,7 +693,11 @@ int config_text_file_save(const char *configfile, const struct ast_config *cfg,
 	}
 	time(&t);
 	ast_copy_string(date, ctime(&t), sizeof(date));
+#ifdef __CYGWIN__	
+	if ((f = fopen(fn, "w+"))) {
+#else
 	if ((f = fopen(fn, "w"))) {
+#endif	    
 		if ((option_verbose > 1) && !option_debug)
 			ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
 		fprintf(f, ";!\n");
diff --git a/cygwin/Makefile b/cygwin/Makefile
new file mode 100755
index 0000000000000000000000000000000000000000..124628f9c711e0c9fce9261f848016526ef2f9ea
--- /dev/null
+++ b/cygwin/Makefile
@@ -0,0 +1,6 @@
+OBJS=cygloader.o
+all: $(OBJS)
+$(OBJS) : %.o: %.c
+	$(CC) $< -o asterisk.exe
+clean:
+	rm -f asterisk.exe
diff --git a/cygwin/cygloader.c b/cygwin/cygloader.c
new file mode 100755
index 0000000000000000000000000000000000000000..0d48c0f6cbc153b9ef6f7237e42cbf5348767263
--- /dev/null
+++ b/cygwin/cygloader.c
@@ -0,0 +1,33 @@
+#include <unistd.h>
+#include <dlfcn.h>
+#include <stdio.h>
+
+#define OK					0
+#define MODULE_NOT_FOUND			1
+#define INVALID_NUMBER_ARGUMENTS	2
+
+int main(int argc, char **argv) {
+	/* Asterisk entry point */
+	char* error = NULL;
+	int (*ast_main)(int argc, char **argv);
+
+	void *handle = dlopen ("asterisk.dll", RTLD_GLOBAL);
+	if (handle == NULL) {
+		fputs (dlerror(), stderr);
+		fputs ("\r\n", stderr);
+		return MODULE_NOT_FOUND;
+	}
+	printf("\r\nAsterisk module loaded successfully");
+	ast_main = dlsym(handle, "main");
+		if ((error = dlerror()) != NULL) {
+			fputs("Asterisk main not found", stderr);
+			fputs(error, stderr);
+			exit(1);
+		}
+	printf("\r\nAsterisk entry point found");
+	/* run asterisk main */
+	(*ast_main)(argc, argv);
+	dlclose(handle);
+	printf("\r\nAsterisk stopped");
+	return OK;
+}
diff --git a/db.c b/db.c
index ae12909fda2b882dee441b325ffeaa102d1937bf..543aa4ddae2c1e631140f03436d46685ba03cfd6 100755
--- a/db.c
+++ b/db.c
@@ -53,6 +53,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/manager.h"
 #include "db1-ast/include/db.h"
 
+#ifdef __CYGWIN__
+#define dbopen __dbopen
+#endif
+
 static DB *astdb;
 AST_MUTEX_DEFINE_STATIC(dblock);
 
diff --git a/formats/Makefile b/formats/Makefile
index 823515c24cbbf671722b54b38ce2be4211bc9a55..31f97339005b81c2cfcf37f96399b1abb882e7b5 100755
--- a/formats/Makefile
+++ b/formats/Makefile
@@ -35,25 +35,29 @@ endif
 
 GSMLIB=../codecs/gsm/lib/libgsm.a
 
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -lasterisk.dll
+else
 CFLAGS+=-fPIC
-
+endif
 all: depend $(FORMAT_LIBS)
 
 clean:
 	rm -f *.so *.o .depend
 
 %.so : %.o
-	$(CC) $(SOLINK) -o $@ $<
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
 
 ifneq ($(wildcard .depend),)
   include .depend
 endif
 
 format_mp3.so : format_mp3.o
-	$(CC) $(SOLINK) -o $@ $< -lm
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lm
 
 format_ogg_vorbis.so : format_ogg_vorbis.o
-	$(CC) $(SOLINK) -o $@ $< -logg -lvorbis -lvorbisenc -lm
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -logg -lvorbis -lvorbisenc -lm
 
 install: all
 	for x in $(FORMAT_LIBS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
diff --git a/funcs/Makefile b/funcs/Makefile
index 7ec63b40708b8adaf0dd349e7f51b085c0de2314..14bbd80a3160d2d78122928ec525aef785f91530 100755
--- a/funcs/Makefile
+++ b/funcs/Makefile
@@ -33,7 +33,12 @@ FUNC_SOURCES=$(BUILTINS:.o=.c)
 
 FUNC_STRUCTS=$(shell grep 'struct ast_custom_function' $(FUNC_SOURCES) | awk '{print $$3};')
 
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -lasterisk.dll
+else
 CFLAGS+=-fPIC
+endif
 
 ifeq ($(findstring BSD,${OSARCH}),BSD)
   CFLAGS+=-I$(CROSS_COMPILE_TARGET)/usr/local/include -L$(CROSS_COMPILE_TARGET)/usr/local/lib
@@ -45,7 +50,7 @@ clean:
 	rm -f *.so *.o .depend pbx_functions.h
 
 %.so : %.o
-	$(CC) $(SOLINK) -o $@ $<
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
 
 #$(BUILTINS) : CFLAGS += -DBUILTIN_FUNC
 
@@ -59,7 +64,7 @@ pbx_functions.h: $(FUNC_SOURCES)
 pbx_functions.o: pbx_functions.h
 
 pbx_functions.so: pbx_functions.o #$(BUILTINS)
-	$(CC) $(SOLINK) -o $@ $<
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
 
 install: all
 	for x in $(FUNCS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
diff --git a/include/asterisk/compat.h b/include/asterisk/compat.h
index fc4791b9cc4e72251567acbdf12063421b523ade..fb6a5d59fa974821ebed713a227c9dbb853b691f 100755
--- a/include/asterisk/compat.h
+++ b/include/asterisk/compat.h
@@ -63,8 +63,12 @@ int unsetenv(const char *name);
 
 #ifdef __CYGWIN__
 #define _WIN32_WINNT 0x0500
-#include <windows.h>
-#include <w32api/ws2tcpip.h>
+#ifndef INET_ADDRSTRLEN
+#define INET_ADDRSTRLEN  16
+#endif
+#ifndef INET6_ADDRSTRLEN
+#define INET6_ADDRSTRLEN 46
+#endif
 #endif /* __CYGWIN__ */
 
 #define HAVE_VASPRINTF
diff --git a/include/asterisk/lock.h b/include/asterisk/lock.h
index 5c6236015d2d6d5640d41cd0be58c466d2ae1154..edc1126986aa46ea994c173bc02cbf63a7125347 100755
--- a/include/asterisk/lock.h
+++ b/include/asterisk/lock.h
@@ -41,6 +41,10 @@
 														 0x20 } }
 #endif
 
+#ifdef __CYGWIN__
+#define PTHREAD_MUTEX_RECURSIVE_NP					PTHREAD_MUTEX_RECURSIVE
+#endif
+
 #ifdef BSD
 #ifdef __GNUC__
 #define AST_MUTEX_INIT_W_CONSTRUCTORS
diff --git a/pbx/Makefile b/pbx/Makefile
index 7af318a93594ff6d9eedd043840a87ab94b34c15..f86ae485904c3320a81b91c91d7c64357f30efdc 100755
--- a/pbx/Makefile
+++ b/pbx/Makefile
@@ -28,7 +28,13 @@ GTK_LIBS=`${CROSS_COMPILE_BIN}gtk-config --libs gthread`
 MOC=$(QTDIR)/bin/moc
 KDE_FLAGS=-I$(KDEDIR)/include -I$(KDEDIR)/include/kde -I$(QTDIR)/include
 KDE_LIBS=-L$(KDEDIR)/lib -L$(QTDIR)/lib -lqt -lkdecore -lkdeui
+
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -L../res -lasterisk.dll 
+else
 CFLAGS+=-fPIC
+endif
 
 KDE_CONSOLE_OBJS=pbx_kdeconsole_main.o pbx_kdeconsole.o 
 
@@ -53,13 +59,13 @@ pbx_kdeconsole.so: $(KDE_CONSOLE_OBJS)
 	$(CC) $(SOLINK) -o $@ $(KDE_CONSOLE_OBJS) $(KDE_LIBS)
 
 pbx_dundi.so: dundi-parser.o pbx_dundi.o
-	$(CC) $(SOLINK) -o $@ pbx_dundi.o dundi-parser.o -lz
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} pbx_dundi.o dundi-parser.o -lz ${CYGSOLIB}
 
 %.moc : %.h
 	$(MOC) $< -o $@
 
 %.so : %.o
-	$(CC) $(SOLINK) -o $@ $<
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
 
 ifneq ($(wildcard .depend),)
   include .depend
diff --git a/res/Makefile b/res/Makefile
index f688b37bf167f4d37d013024c8d96b468184f1a0..b4170e397c00b22b8c00f62304d4abe188166429 100755
--- a/res/Makefile
+++ b/res/Makefile
@@ -11,8 +11,7 @@
 # the GNU General Public License
 #
 
-MODS=res_adsi.so res_features.so res_musiconhold.so res_indications.so res_monitor.so \
-     res_agi.so
+MODS=res_indications.so res_monitor.so res_adsi.so res_agi.so res_features.so
 
 ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/odbcinst.h)$(wildcard $(CROSS_COMPILE_TARGET)/usr/local/include/odbcinst.h),)
   ifneq (${OSARCH},FreeBSD)
@@ -41,6 +40,15 @@ ifeq ($(findstring BSD,${OSARCH}),BSD)
   CFLAGS+=-I$(CROSS_COMPILE_TARGET)/usr/local/include -L$(CROSS_COMPILE_TARGET)/usr/local/lib
 endif
 
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -lasterisk.dll
+CYG_RES_CONFIG_ODBC_LIB=-lres_odbc.so
+CYG_RES_FEATURES_LIB=-lres_adsi.so -lres_monitor.so
+else
+MODS+=res_musiconhold.so 
+endif
+
 CRYPTO_LIBS=-lssl -lcrypto
 
 CFLAGS+=
@@ -52,7 +60,11 @@ endif
 #
 # Work around buggy RedHat 9.0
 #
+ifeq (${OSARCH},CYGWIN)
+CFLAGS+=-DOPENSSL_NO_KRB5
+else
 CFLAGS+=-DOPENSSL_NO_KRB5 -fPIC
+endif
 
 all: depend $(MODS)
 
@@ -63,19 +75,25 @@ install: all
 	for x in $(MODS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
 
 res_crypto.so: res_crypto.o
-	$(CC) $(SOLINK) -o $@ $< $(CRYPTO_LIBS)
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(CRYPTO_LIBS)
 
 clean:
 	rm -f *.so *.o .depend
 
 res_odbc.so: res_odbc.o
-	$(CC) $(SOLINK) -o $@ $< -lodbc
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lodbc
 
 res_osp.so: res_osp.o $(OSPLIB)
-	$(CC) $(SOLINK) -o $@ $^
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(OSPLIB)
 
 %.so : %.o
-	$(CC) $(SOLINK) -o $@ $<
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
+
+res_features.so: res_features.o
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} ${CYG_RES_FEATURES_LIB}
+
+res_config_odbc.so: res_config_odbc.o
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} ${CYG_RES_CONFIG_ODBC_LIB}
 
 ifneq ($(wildcard .depend),)
   include .depend
diff --git a/stdtime/private.h b/stdtime/private.h
index 90329d3fa70b0b5049bc7ad93e448b9d89bb9f2e..60479d787f0a36228801bcee753c086a704d62f1 100755
--- a/stdtime/private.h
+++ b/stdtime/private.h
@@ -9,9 +9,11 @@
 */
 
 /* Stuff moved from Makefile.inc to reduce clutter */
+#ifndef __CYGWIN__
 #ifndef TM_GMTOFF
 #define TM_GMTOFF	tm_gmtoff
 #define TM_ZONE		tm_zone
+#endif
 #define STD_INSPIRED	1
 #define PCTS		1
 #define HAVE_LONG_DOUBLE 1
diff --git a/utils.c b/utils.c
index d3292ff908ab875c63801908c4bba41b8812b634..9dffe253a599930c6004e0ea759e42622fd77432 100755
--- a/utils.c
+++ b/utils.c
@@ -44,6 +44,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/logger.h"
 #include "asterisk/md5.h"
 #include "asterisk/options.h"
+#include "asterisk/compat.h"
 
 #define AST_API_MODULE		/* ensure that inlinable API functions will be built in this module if required */
 #include "asterisk/strings.h"
@@ -57,7 +58,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 static char base64[64];
 static char b2a[256];
 
-#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__)
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__)
 
 /* duh? ERANGE value copied from web... */
 #define ERANGE 34
@@ -741,10 +742,14 @@ int vasprintf(char **strp, const char *fmt, va_list ap)
 #endif /* !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
 
 #ifndef HAVE_STRTOQ
+#ifndef LONG_MIN
 #define LONG_MIN        (-9223372036854775807L-1L)
 	                                 /* min value of a "long int" */
+#endif
+#ifndef LONG_MAX
 #define LONG_MAX        9223372036854775807L
 	                                 /* max value of a "long int" */
+#endif
 
 /*
  * Convert a string to a quad integer.
diff --git a/utils/streamplayer.c b/utils/streamplayer.c
index 4cd953114e804fc4189e961adb94af1aefae9ab2..24f3142b8a11d941e566a3804ff104452a2d2a87 100755
--- a/utils/streamplayer.c
+++ b/utils/streamplayer.c
@@ -31,7 +31,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__Darwin__)
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__Darwin__) || defined(__CYGWIN__)
 #include <netinet/in.h>
 #endif
 #include <sys/time.h>