summaryrefslogtreecommitdiff
path: root/protocols/SkypeClassic/src/skypeproxy/skypeproxy.c
blob: 65ba2e0ccc04da9bd0c798eabe04058df786fd31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* 

Purpose
=======
This program opens a connection on a local TCP/IP port and sends/
receives Skype-API calls on it so that you can remote-control
Skype over a network.
Note, that there are currently NO SECURITY mechanisms, so don't
use this over an untrusted network!
  
Author
======
This program was written by leecher in 2005 (mailto:leecher@dose.0wnz.at)
Please give feedback at http://forum.skype.com/viewtopic.php?t=16187

Protocol
========
Basic protocol structure
------------------------
Sender and receiver have the same protocol:

  [(UINT)Length of message][(char[])Message]

The Length is so that you can malloc() enough space for the data buffer
to receive the next message.

A special case is, if the [Length of message] is 0. In this case the
client tells the server that he wants to switch to command mode.

Command mode
------------
The server expects

  [(char)Command]

next. Currently the following commands are supported:

  CAPABILITIES		- returns the Server's capabilities
  AUTHENTICATE		- Starts the authentication process

CAPABILITIES
------------
The server returns

  [(char)Capabilities]

where this currently can be the following:

  USE_AUTHENTICATION - The server supports and requires authentication

AUTHENTICATE
------------
The server returns

  [(char)0x01]

if authentication is supported AND needed (skypeproxy started with -k switch) or

  [(char)0x00]

if this is not the case.
If 0x01 was returned the server next expects a normal message 
(see "Basic protocol structure) containing the password. 
If the authentication was successful, the server replies with

  [(char)0x01]

otherwise with

  [(char)0x00]

PLEASE NOTE THAT THE AUTHENTICATION CURRENTLY IS PLAIN TEXT. SO DON'T
USE THIS PROGRAM OVER AN UNTRUSTED NETWORK, OTHERWISE THERE MAY BE THE
POSSIBILITY THAT SOMEONE SNIFFS YOUR PASSWORD!

Code example
------------

SOCKET MySocket;

int SendPacket(char *szSkypeMessage) {
	unsigned int length=strlen(szSkypeMsg);

	if (send(MySocket, (char *)&length, sizeof(length), 0)==SOCKET_ERROR ||
		send(MySocket, szSkypeMsg, length, 0)==SOCKET_ERROR) 
			return -1;
	return 0;
}

// don't forget to free() the return value on my Heap!!
char *ReceivePacket(void) {
	unsigned int lenght, received;
	char *buf;

	if ((received=recv(MySocket, (char *)&length, sizeof(length), 0))==SOCKET_ERROR ||
		received==0) 
			return NULL;
	if (!(buf=calloc(1, length+1))) return NULL;
	if (recv(MySocket, buf, length, 0)==SOCKET_ERROR) {
		free(buf);
		return NULL;
	}
	return buf;
}	


License
=======
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

History
========
V1.0alpha - First preview release
V1.0	  - Implemented killing & restarting Skype process when it dies
		  - BUGFIX: SendMessage() is a blocking call, if Skype hangs our app hangs too -> Fixed
		  - Added command line parsing.
		  - Renamed from Skype2Socket to skypeproxy
		  - Added authentication feature.
*/

#include <stdio.h>
#include <process.h>
#include <windows.h>
#include <signal.h>
#include "skypeproxy.h"

UINT ControlAPIAttach, ControlAPIDiscover;
HWND hSkypeWnd=NULL, hWnd;
HANDLE SkypeReady, ServerThreadBye;
LONG AttachStatus=-1;
int exitcode=EXIT_SUCCESS;
char skype_path[MAX_PATH], *password=NULL;
BYTE WatchDog=1;
BOOL WatchDogRunning=FALSE, Authenticated=FALSE;
SOCKET ListenSocket, AcceptSocket;


void bail_out(int i) {
	OUTPUT("Got termination signal, bailing out.");
	if (i==1) exitcode=EXIT_FAILURE;
	PostMessage(hWnd, WM_QUIT, 0, 0);
}

BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam ) {
  DWORD dwID ;

  GetWindowThreadProcessId(hwnd, &dwID) ;
  if(dwID == (DWORD)lParam)
	PostMessage(hwnd, WM_CLOSE, 0, 0) ;	// May you be so kind to quit, please?

  return TRUE ;
}

/* ConnectToSkypeAPI
 * 
 * Purpose: Establish a connection to the Skype API
 * Params : ForceRestart - Kill Skype if it's running before restarting
 * Returns: 0 - Connecting succeeded
 *		   -1 - Something went wrong
 */
void ConnectToSkypeAPI(void *ForceRestart) {
	BOOL SkypeLaunched=FALSE;
	int counter=0, i, j;
	char *args[5];
	char *SkypeOptions[]={"/notray", "/nosplash", "/minimized"};
	char *szFuncName="ConnectToSkypeAPI";

	ResetEvent(SkypeReady);
	AttachStatus=-1;
	if ((BOOL)ForceRestart) {
		HANDLE hProc;
		DWORD dwPID=0;

		if (!hSkypeWnd) {
			OUTPUT("I can't kill Skype, as I don't even have its window handle!");
			return;
		}
		GetWindowThreadProcessId(hSkypeWnd, &dwPID);
		LOG(("%s: Shutting down Skype as it was not behaving the way it should...", szFuncName));
		if (hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, dwPID)) {

			// Try to shutdown Skype the nice way by asking it to close
			EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID);

			if(WaitForSingleObject(hProc, 10000)!=WAIT_OBJECT_0) {
				// Try it the hard way by killing it
				LOG(("%s: I tried it the nice way, but you were not listening! Now DIIIIEEE!", szFuncName));
				if (!TerminateProcess(hProc,0)) {
					OUTPUT("Argh, process refused to die, it's too mighty for me, I've given up");
					CloseHandle(hProc);
					return;
				}
				LOG(("%s: Process killed! >:)", szFuncName));
			}
			CloseHandle(hProc);
		}
	}
	do {
		/*	To initiate communication, Client should broadcast windows message
			('SkypeControlAPIDiscover') to all windows in the system, specifying its own
			window handle in wParam parameter.
		 */
		LOG(("%s: Sending discover message..", szFuncName));
		SendMessageTimeout(HWND_BROADCAST, ControlAPIDiscover, (WPARAM)hWnd, 0, SMTO_ABORTIFHUNG, 3000, NULL);
		LOG(("%s: Discover message sent, waiting for Skype to become ready..", szFuncName));

		/*	In response, Skype responds with
			message 'SkypeControlAPIAttach' to the handle specified, and indicates
			connection status
			SkypeReady is set if there is an answer by Skype other than API_AVAILABLE.
			If there is no answer after 3 seconds, launch Skype as it's propably
			not running.
		*/
		if (WaitForSingleObject(SkypeReady, 3000)==WAIT_TIMEOUT &&
			AttachStatus!=SKYPECONTROLAPI_ATTACH_PENDING_AUTHORIZATION) 
		{
			if (hWnd==NULL) {
				LOG(("%s: hWnd of SkypeDispatchWindow not yet set..", szFuncName));
				continue;
			}
			if (!SkypeLaunched && skype_path) {
				LOG(("%s: Starting Skype, as it's not running", szFuncName));
				args[0]=skype_path;
				j=1;
				for (i=0; i<3; i++) {
					args[j]=SkypeOptions[i];
					LOG(("%s: Using Skype parameter: ", szFuncName, args[j]));
					j++;
				}
				args[j]=NULL;
				_spawnv(_P_NOWAIT, skype_path, args);
				ResetEvent(SkypeReady);
				SkypeLaunched=TRUE;
				LOG(("%s: Skype process started.", szFuncName));
				// Skype launching iniciated, keep sending Discover messages until it responds.
				continue;
			} else {
				LOG(("%s: Check if Skype was launchable..", szFuncName));
				if (!skype_path) {
					OUTPUT("There was no correct path for Skype application");
					bail_out(1);
					return;
				}
				LOG("%s: Trying to attach: #%d", szFuncName, counter));
				counter++;
				if (counter==5) {
					OUTPUT("ERROR: Skype not running / too old / working!");
					bail_out(1);
					return;
				}
			}
		}
		LOG(("%s: Attachstatus %d", szFuncName, AttachStatus));
	} while (AttachStatus==SKYPECONTROLAPI_ATTACH_API_AVAILABLE || AttachStatus==-1);
	
	while (AttachStatus==SKYPECONTROLAPI_ATTACH_PENDING_AUTHORIZATION) Sleep(1000);
	LOG(("%s: Attachstatus %d", szFuncName, AttachStatus));
	if (AttachStatus!=SKYPECONTROLAPI_ATTACH_SUCCESS) {
		switch(AttachStatus) {
			case SKYPECONTROLAPI_ATTACH_REFUSED:
				OUTPUT("Skype refused the connection :(");
				break;
			case SKYPECONTROLAPI_ATTACH_NOT_AVAILABLE:
				OUTPUT("The Skype API is not available");
				break;
			default:
				LOG(("%s: ERROR: AttachStatus: %d", szFuncName, AttachStatus));
				OUTPUT("Wheee, Skype won't let me use the API. :(");
		}
		bail_out(1);
		return;
	}
	OUTPUT("Attached to Skype successfully.");
	if (!WatchDogRunning)
		if (_beginthread(WatchDogTimer, 0, NULL)==-1) {
			OUTPUT("Cannot start Watchdog.");
			bail_out(1);
		}
	return;
}

void SkypeSend(char *szMsg) {
   COPYDATASTRUCT CopyData;
   int count=0;

   if (!hSkypeWnd) {
	   LOG(("SkypeSend: DAMN! No Skype window handle! :("));
	   return;
   }
   if (mir_strcmp(szMsg, "PING")) {LOG(("> %s", szMsg));}
   CopyData.dwData=0; 
   CopyData.lpData=szMsg; 
   CopyData.cbData=strlen(szMsg)+1;
   while (!SendMessageTimeout(hSkypeWnd, WM_COPYDATA, (WPARAM)hWnd, (LPARAM)&CopyData, SMTO_ABORTIFHUNG, 3000, NULL)) {
	   count++;
	   LOG(("SkypeSend: failed, try #%d", count));
	   if (count==5) {
		   OUTPUT("Sending message to Skype failed too often.");
		   OUTPUT("Skype may have died unexpectedly, I will try to restart it.");
		   ConnectToSkypeAPI((void *)TRUE);
		   OUTPUT("Restart complete. Trying to deliver message one more time.");
		   if (!SendMessageTimeout(hSkypeWnd, WM_COPYDATA, (WPARAM)hWnd, (LPARAM)&CopyData, SMTO_ABORTIFHUNG, 3000, NULL)) {
			   OUTPUT("It still failed. Skype seems to be completely f*cked up. I've given up. Bye..");
			   bail_out(1);
			   break;
		   } else { 
			   OUTPUT("Now it worked! :)");
			   break;
		   }
	   }
	   Sleep(1000);
   }
}

void ServerThread(char *dummy) {
	unsigned int length, received;
	char *buf, command, reply;

	LOG(("ServerThread started"));
	AcceptSocket=INVALID_SOCKET;
	while( AcceptSocket == INVALID_SOCKET) {
		if ((AcceptSocket = accept( ListenSocket, NULL, NULL ))==INVALID_SOCKET) {
			LOG(("ServerThread: Byebye..."));
			SetEvent(ServerThreadBye);
			bail_out(1);
			return;
		}
		OUTPUT("Connection by client");
		while(1) {
			if ((received=recv(AcceptSocket, (char *)&length, sizeof(length), 0))==SOCKET_ERROR ||
				received==0) 
			{
				OUTPUT("Connection was closed by client. See ya soon! :)");
				break;
			}
			// Command mode
			if (length==0) {
				reply=0;
				if (recv(AcceptSocket, (char *)&command, 1, 0)==SOCKET_ERROR) {
					OUTPUT("Connection to client was lost.");
					break;
				}
#ifdef USE_AUTHENTICATION
				if (command==AUTHENTICATE)
					if (password) reply=0x01;		 // Ok, go ahead
					else command=0;
#endif
				if (command==CAPABILITIES)
					reply=password?USE_AUTHENTICATION:0;

				if (send(AcceptSocket, (char *)&reply, 1, 0)==SOCKET_ERROR) {
					OUTPUT("Connection to client was lost.");
					break;
				}
				continue;
			}
			// Normal Skype API-call
			if (!(buf=calloc(1, length+1))) {
				OUTPUT("Out of memory error while allocating buffer space.");
				break;
			}
			if (recv(AcceptSocket, buf, length, 0)==SOCKET_ERROR) {
				OUTPUT("Connection to client was lost.");
				free(buf);
				break;
			}
			switch (command) {
#ifdef USE_AUTHENTICATION
			case 0x01: // Compare hash
				if (password && !mir_strcmp(password, buf)) Authenticated=TRUE;
				else Authenticated=FALSE;
				if (Authenticated) {
					OUTPUT("User authenticated succesfully.");
					reply=1; 
				} else {
					OUTPUT("User authentication failed!! (Intruder?)");
					reply=0;
				}
				if (send(AcceptSocket, (char *)&reply, 1, 0)==SOCKET_ERROR) {
					OUTPUT("Connection to client was lost.");
					break;
				}
				command=0;
				break;
#endif
			default:
#ifdef USE_AUTHENTICATION
				if (password && !Authenticated) break;
#endif
				SkypeSend(buf);
			}
			command=0;
			free(buf);
		}
		AcceptSocket=INVALID_SOCKET;
#ifdef USE_AUTHENTICATION
		Authenticated=FALSE;
#endif
	}
}


void WatchDogTimer(char *dummy) {
	LOG(("WatchDogTimer started"));
	WatchDogRunning=TRUE;
	while (1) {
		Sleep(PING_INTERVAL);
		if (!WatchDog) {
			OUTPUT("Ouch.. It seems that Skype has died unexpectedly. Trying to restart.");
			ConnectToSkypeAPI((void *)TRUE);
		}
		WatchDog=0;
		SkypeSend("PING");
	}
}

LONG APIENTRY WndProc(HWND hWnd, UINT message, UINT wParam, LONG lParam) 
{ 
    PCOPYDATASTRUCT CopyData; 
	char *szSkypeMsg=NULL;

    switch (message) 
    { 
        case WM_COPYDATA: 
//		 LOG("WM_COPYDATA", "start");
		 if(hSkypeWnd==(HWND)wParam) { 
			CopyData=(PCOPYDATASTRUCT)lParam;
			szSkypeMsg=strdup(CopyData->lpData);
			ReplyMessage(1);
			if (!mir_strcmp(szSkypeMsg, "PONG")) {
				WatchDog=1;
				break;
			} // Hide PING-PONG
			LOG(("< %s", szSkypeMsg));
			if (!mir_strcmp(szSkypeMsg, "USERSTATUS LOGGEDOUT")) {
				OUTPUT("Skype shut down gracefully. I'll leave too, bye.. :)");
				bail_out(1);
			}
#ifdef USE_AUTHENTICATION
			if (password && !Authenticated) break;
#endif
			if (AcceptSocket!=INVALID_SOCKET) {
				unsigned int length=strlen(szSkypeMsg);

				if (send(AcceptSocket, (char *)&length, sizeof(length), 0)==SOCKET_ERROR ||
					send(AcceptSocket, szSkypeMsg, length, 0)==SOCKET_ERROR) 
					OUTPUT("Cannot send to client :(");
			}
		 }
        break; 

        case WM_DESTROY: 
            PostQuitMessage(0); 
        break; 

        default: 
		 if(message==ControlAPIAttach) {
				// Skype responds with Attach to the discover-message
				AttachStatus=lParam;
				if (AttachStatus==SKYPECONTROLAPI_ATTACH_SUCCESS) 
					hSkypeWnd=(HWND)wParam;	   // Skype gave us the communication window handle
				if (AttachStatus!=SKYPECONTROLAPI_ATTACH_API_AVAILABLE)
					SetEvent(SkypeReady);
				break;
		 }
		 return (DefWindowProc(hWnd, message, wParam, lParam)); 
    }
//	LOG("WM_COPYDATA", "exit");
	if (szSkypeMsg) free(szSkypeMsg);
	return 1;
} 


void TellError(DWORD err) {
	LPVOID lpMsgBuf;
	
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
					  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
        MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
        LocalFree( lpMsgBuf );
		return;
}

int main(int argc, char *argv[]) {
	DWORD Buffsize;
	HKEY MyKey;
	BOOL SkypeInstalled=TRUE;
    MSG Message; 
	WNDCLASS WndClass; 
	SOCKADDR_IN service;
	WSADATA wsaData;
	int ExitCode=STILL_ACTIVE;
	unsigned short BindPort=1401;
	char BindIP[16]="0.0.0.0";
	
	printf("Skypeproxy V1.0, by leecher 2005 <leecher@dose.0wnz.at>\n\n");

	if (argc>1) {
		int i;

		if (!stricmp(argv[1], "-h") || !stricmp(argv[1], "--help") || !stricmp(argv[1], "/?")) {
			printf("Usage: %s [-i BindIP] [-p BindPort]", argv[0]);
#ifdef USE_AUTHENTICATION
			printf(" [-k Password]");
#endif
			printf("\n\n");
			return EXIT_SUCCESS;
		}
		for (i=0;i<argc;i++) {
			if (!stricmp(argv[i], "-i") && argc>i+1)
				strncpy(BindIP, argv[i+1], sizeof(BindIP));
			if (!stricmp(argv[i], "-p") && argc>i+1) 
				if (!(BindPort=atoi(argv[i+1]))) {
					OUTPUT("ERROR: Cannot convert port to int. bye..");
					return EXIT_FAILURE;
				}
#ifdef USE_AUTHENTICATION
			if (!stricmp(argv[i], "-k") && argc>i+1)
				password=strdup(argv[i+1]);
#endif
		}
	}

	if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Skype\\Phone", 0, KEY_READ, &MyKey)!=ERROR_SUCCESS ||
		RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Skype\\Phone", 0, KEY_READ, &MyKey)!=ERROR_SUCCESS)
			SkypeInstalled=FALSE;
	Buffsize=sizeof(skype_path);
	if (SkypeInstalled==FALSE || 
		RegQueryValueEx(MyKey, "SkypePath", NULL, NULL, skype_path,  &Buffsize)!=ERROR_SUCCESS) {
		    OUTPUT("Skype was not found on this machine :(");
			RegCloseKey(MyKey);
			skype_path[0]=0;
			return EXIT_FAILURE;
	}
	RegCloseKey(MyKey);

	if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
		OUTPUT("Error at loading windows sockets.");
		return EXIT_FAILURE;
	}

	if ((ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
		printf("* Error at creating socket(): Error %d", WSAGetLastError());
		return EXIT_FAILURE;
	}

	service.sin_family = AF_INET;
	service.sin_addr.s_addr = inet_addr(BindIP);
	service.sin_port = htons(BindPort);

	printf("* Binding to interface %s, Port %d..", BindIP, BindPort);
	if (bind( ListenSocket, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR ||
		listen( ListenSocket, 1 ) == SOCKET_ERROR)
	{
		OUTPUT("Failed.");
		closesocket(ListenSocket);
		return EXIT_FAILURE;
	}
	printf("OK\n");


	if (!(ControlAPIAttach=RegisterWindowMessage("SkypeControlAPIAttach")) ||
		!(ControlAPIDiscover=RegisterWindowMessage("SkypeControlAPIDiscover"))) {
			OUTPUT("Cannot register Windows message.");
			closesocket(ListenSocket);
			return EXIT_FAILURE;
	}
	
	// Create window class
	hSkypeWnd=NULL;
    WndClass.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; 
    WndClass.lpfnWndProc = (WNDPROC)WndProc; 
    WndClass.cbClsExtra = 0; 
    WndClass.cbWndExtra = 0; 
    WndClass.hInstance =  NULL;
    WndClass.hIcon = NULL; 
    WndClass.hCursor = NULL;
    WndClass.hbrBackground = NULL;
    WndClass.lpszMenuName = NULL; 
    WndClass.lpszClassName = "SkypeApiDispatchWindow"; 
    RegisterClass(&WndClass);
	// Do not check the retval of RegisterClass, because on non-unicode
	// win98 it will fail, as it is a stub that returns false() there
	
	// Create main window
	hWnd=CreateWindowEx( WS_EX_APPWINDOW|WS_EX_WINDOWEDGE,
		"SkypeApiDispatchWindow", "", WS_BORDER|WS_SYSMENU|WS_MINIMIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT, 128, 128, NULL, 0, (HINSTANCE)WndClass.hInstance, 0);

    if (!hWnd) {
		OUTPUT("Cannot create window.");
		TellError(GetLastError());
		closesocket(ListenSocket);
		CloseHandle(WndClass.hInstance);
        return EXIT_FAILURE; 
	}
    ShowWindow(hWnd, 0); 
    UpdateWindow(hWnd); 

	if (!(SkypeReady=CreateEvent(NULL, TRUE, FALSE, NULL)) ||
		!(ServerThreadBye=CreateEvent(NULL, TRUE, FALSE, NULL))) {
		 OUTPUT("Unable to create Mutex!");
		 closesocket(ListenSocket);
		 CloseHandle(WndClass.hInstance);
	 	 return EXIT_FAILURE;
	}

	if (_beginthread(ConnectToSkypeAPI, 0, (void *)FALSE)==-1 ||
		_beginthread(ServerThread, 0, NULL)==-1) {
		OUTPUT("Cannot create thread. Bye..");
		closesocket(ListenSocket);
		CloseHandle(WndClass.hInstance);
		CloseHandle(SkypeReady);
		return EXIT_FAILURE;
	}

	signal(SIGINT, &bail_out);
	LOG(("Startup: Messagepump started.\nPress CTRL+C to terminate\n"));

	while (GetMessage(&Message, hWnd, 0, 0)) 
    { 
        TranslateMessage(&Message); 
        DispatchMessage(&Message);
    }
	
	LOG(("Shutdown: Messagepump stopped"));

	if (password) free(password);
	if (AcceptSocket != INVALID_SOCKET) closesocket(AcceptSocket);
	closesocket(ListenSocket);
	LOG(("Shutdown: Waiting for serverthread to quit..."));
	if (WaitForSingleObject(ServerThreadBye, 3000)==WAIT_TIMEOUT)
		{OUTPUT("Serverthread didn't terminate correctly, shutting down anyway...");}
	else
		{LOG(("ServerThread terminated"));}
	CloseHandle(WndClass.hInstance);
	CloseHandle(SkypeReady);
	return exitcode;
}