File: socket.cpp

package info (click to toggle)
megaglest 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,904 kB
  • ctags: 18,215
  • sloc: cpp: 144,232; ansic: 11,860; sh: 2,949; perl: 1,899; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (3203 lines) | stat: -rw-r--r-- 135,409 bytes parent folder | download
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
//This file is part of Glest Shared Library (www.glest.org)
//Copyright (C) 2005 Matthias Braun <matze@braunis.de>

//You can redistribute this code 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.

#include "socket.h"

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>

#if defined(HAVE_SYS_IOCTL_H) || defined(__linux__)
  #define BSD_COMP /* needed for FIONREAD on Solaris2 */
  #include <sys/ioctl.h>
#endif
#if defined(HAVE_SYS_FILIO_H) /* needed for FIONREAD on Solaris 2.5 */
  #include <sys/filio.h>
#endif

#include "conversion.h"
#include "util.h"
#include "platform_util.h"
#include <algorithm>

#ifdef WIN32

  #include <windows.h>
  #include <winsock2.h>
  #include <winsock.h>
  #include <iphlpapi.h>
  #include <strstream>
  #include <strsafe.h>

#define MSG_NOSIGNAL 0
#define MSG_DONTWAIT 0

#else

  #include <unistd.h>
  #include <stdlib.h>
  #include <sys/socket.h>
  #include <netdb.h>
  #include <netinet/in.h>
  #include <net/if.h>
  #include <netinet/tcp.h>
#endif


#include <string.h>
#include <sys/stat.h>
#include <time.h>

#include "miniwget.h"
#include "miniupnpc.h"
#include "upnpcommands.h"

#include "leak_dumper.h"

using namespace std;
using namespace Shared::Util;

namespace Shared{ namespace Platform{

bool Socket::disableNagle = false;
int Socket::DEFAULT_SOCKET_SENDBUF_SIZE = -1;
int Socket::DEFAULT_SOCKET_RECVBUF_SIZE = -1;
string Socket::host_name = "";

int Socket::broadcast_portno    = 61357;
int ServerSocket::ftpServerPort = 61358;
int ServerSocket::maxPlayerCount = -1;
int ServerSocket::externalPort  = Socket::broadcast_portno;
BroadCastClientSocketThread *ClientSocket::broadCastClientThread = NULL;
SDL_Thread *ServerSocket::upnpdiscoverThread = NULL;
bool ServerSocket::cancelUpnpdiscoverThread = false;
Mutex ServerSocket::mutexUpnpdiscoverThread;
//
// UPnP - Start
//
static struct UPNPUrls urls;
static struct IGDdatas data;
// local ip address
static char lanaddr[16]         = "";
bool UPNP_Tools::isUPNP         = true;
bool UPNP_Tools::enabledUPNP    = false;
Mutex UPNP_Tools::mutexUPNP;
// UPnP - End

#ifdef WIN32

    #define socklen_t 	int
	#define MAXHOSTNAME 254

//#define PLATFORM_SOCKET_TRY_AGAIN WSAEWOULDBLOCK
//#define PLATFORM_SOCKET_INPROGRESS WSAEINPROGRESS
//#define PLATFORM_SOCKET_INTERRUPTED WSAEWOULDBLOCK
	typedef SSIZE_T ssize_t;

	//// Constants /////////////////////////////////////////////////////////
	const int kBufferSize = 1024;

	//// Statics ///////////////////////////////////////////////////////////
	// List of Winsock error constants mapped to an interpretation string.
	// Note that this list must remain sorted by the error constants'
	// values, because we do a binary search on the list when looking up
	// items.

	static class ErrorEntry
	{
	public:
		int nID;
		const char* pcMessage;

		ErrorEntry(int id, const char* pc = 0) : nID(id), pcMessage(pc)
		{
		}

		bool operator<(const ErrorEntry& rhs)
		{
			return nID < rhs.nID;
		}

	} gaErrorList[] =
	  {
		ErrorEntry(0,                  "No error"),
		ErrorEntry(WSAEINTR,           "Interrupted system call"),
		ErrorEntry(WSAEBADF,           "Bad file number"),
		ErrorEntry(WSAEACCES,          "Permission denied"),
		ErrorEntry(WSAEFAULT,          "Bad address"),
		ErrorEntry(WSAEINVAL,          "Invalid argument"),
		ErrorEntry(WSAEMFILE,          "Too many open sockets"),
		ErrorEntry(WSAEWOULDBLOCK,     "Operation would block"),
		ErrorEntry(WSAEINPROGRESS,     "Operation now in progress"),
		ErrorEntry(WSAEALREADY,        "Operation already in progress"),
		ErrorEntry(WSAENOTSOCK,        "Socket operation on non-socket"),
		ErrorEntry(WSAEDESTADDRREQ,    "Destination address required"),
		ErrorEntry(WSAEMSGSIZE,        "Message too long"),
		ErrorEntry(WSAEPROTOTYPE,      "Protocol wrong type for socket"),
		ErrorEntry(WSAENOPROTOOPT,     "Bad protocol option"),
		ErrorEntry(WSAEPROTONOSUPPORT, "Protocol not supported"),
		ErrorEntry(WSAESOCKTNOSUPPORT, "Socket type not supported"),
		ErrorEntry(WSAEOPNOTSUPP,      "Operation not supported on socket"),
		ErrorEntry(WSAEPFNOSUPPORT,    "Protocol family not supported"),
		ErrorEntry(WSAEAFNOSUPPORT,    "Address family not supported"),
		ErrorEntry(WSAEADDRINUSE,      "Address already in use"),
		ErrorEntry(WSAEADDRNOTAVAIL,   "Can't assign requested address"),
		ErrorEntry(WSAENETDOWN,        "Network is down"),
		ErrorEntry(WSAENETUNREACH,     "Network is unreachable"),
		ErrorEntry(WSAENETRESET,       "Net connection reset"),
		ErrorEntry(WSAECONNABORTED,    "Software caused connection abort"),
		ErrorEntry(WSAECONNRESET,      "Connection reset by peer"),
		ErrorEntry(WSAENOBUFS,         "No buffer space available"),
		ErrorEntry(WSAEISCONN,         "Socket is already connected"),
		ErrorEntry(WSAENOTCONN,        "Socket is not connected"),
		ErrorEntry(WSAESHUTDOWN,       "Can't send after socket shutdown"),
		ErrorEntry(WSAETOOMANYREFS,    "Too many references, can't splice"),
		ErrorEntry(WSAETIMEDOUT,       "Connection timed out"),
		ErrorEntry(WSAECONNREFUSED,    "Connection refused"),
		ErrorEntry(WSAELOOP,           "Too many levels of symbolic links"),
		ErrorEntry(WSAENAMETOOLONG,    "File name too long"),
		ErrorEntry(WSAEHOSTDOWN,       "Host is down"),
		ErrorEntry(WSAEHOSTUNREACH,    "No route to host"),
		ErrorEntry(WSAENOTEMPTY,       "Directory not empty"),
		ErrorEntry(WSAEPROCLIM,        "Too many processes"),
		ErrorEntry(WSAEUSERS,          "Too many users"),
		ErrorEntry(WSAEDQUOT,          "Disc quota exceeded"),
		ErrorEntry(WSAESTALE,          "Stale NFS file handle"),
		ErrorEntry(WSAEREMOTE,         "Too many levels of remote in path"),
		ErrorEntry(WSASYSNOTREADY,     "Network system is unavailable"),
		ErrorEntry(WSAVERNOTSUPPORTED, "Winsock version out of range"),
		ErrorEntry(WSANOTINITIALISED,  "WSAStartup not yet called"),
		ErrorEntry(WSAEDISCON,         "Graceful shutdown in progress"),
		ErrorEntry(WSAHOST_NOT_FOUND,  "Host not found"),
		ErrorEntry(WSANO_DATA,         "No host data of that type was found")
	};

	bool operator<(const ErrorEntry& rhs1,const ErrorEntry& rhs2)
	{
		return rhs1.nID < rhs2.nID;
	}

	const int kNumMessages = sizeof(gaErrorList) / sizeof(ErrorEntry);

	//// WSAGetLastErrorMessage ////////////////////////////////////////////
	// A function similar in spirit to Unix's perror() that tacks a canned
	// interpretation of the value of WSAGetLastError() onto the end of a
	// passed string, separated by a ": ".  Generally, you should implement
	// smarter error handling than this, but for default cases and simple
	// programs, this function is sufficient.
	//
	// This function returns a pointer to an internal static buffer, so you
	// must copy the data from this function before you call it again.  It
	// follows that this function is also not thread-safe.
	const char* WSAGetLastErrorMessage(const char* pcMessagePrefix,
									   int nErrorID = 0 )
	{
		// Build basic error string
		static char acErrorBuffer[8096];
		std::ostrstream outs(acErrorBuffer, 8095);
		outs << pcMessagePrefix << ": ";

		// Tack appropriate canned message onto end of supplied message
		// prefix. Note that we do a binary search here: gaErrorList must be
		// sorted by the error constant's value.
		ErrorEntry* pEnd = gaErrorList + kNumMessages;
		ErrorEntry Target(nErrorID ? nErrorID : WSAGetLastError());
		ErrorEntry* it = std::lower_bound(gaErrorList, pEnd, Target);
		if ((it != pEnd) && (it->nID == Target.nID))
		{
			outs << it->pcMessage;
		}
		else
		{
			// Didn't find error in list, so make up a generic one
			outs << "unknown socket error";
		}
		outs << " (" << Target.nID << ")";

		// Finish error message off and return it.
		outs << std::ends;
		acErrorBuffer[8095] = '\0';
		return acErrorBuffer;
	}

	// keeps in scope for duration of the application
	SocketManager Socket::wsaManager;

	SocketManager::SocketManager() {
		WSADATA wsaData;
		WORD wVersionRequested = MAKEWORD(2, 0);
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("SocketManager calling WSAStartup...\n");
		WSAStartup(wVersionRequested, &wsaData);
		//dont throw exceptions here, this is a static initializacion
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Winsock initialized.\n");
	}

	SocketManager::~SocketManager() {
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("SocketManager calling WSACleanup...\n");
		WSACleanup();
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Winsock cleanup complete.\n");
	}

#else

	typedef unsigned int UINT_PTR, *PUINT_PTR;
	typedef UINT_PTR        SOCKET;
	#define INVALID_SOCKET  (SOCKET)(~0)

	//#define PLATFORM_SOCKET_TRY_AGAIN EAGAIN
	//#define PLATFORM_SOCKET_INPROGRESS EINPROGRESS
	//#define PLATFORM_SOCKET_INTERRUPTED EINTR

#endif

int Socket::getLastSocketError() {
#ifndef WIN32
	return errno;
#else
	return WSAGetLastError();
#endif
}

const char * Socket::getLastSocketErrorText(int *errNumber) {
	int errId = (errNumber != NULL ? *errNumber : getLastSocketError());
#ifndef WIN32
	return strerror(errId);
#else
	return WSAGetLastErrorMessage("",errId);
#endif
}

string Socket::getLastSocketErrorFormattedText(int *errNumber) {
	int errId = (errNumber != NULL ? *errNumber : getLastSocketError());
	string msg = "(Error: " + intToStr(errId) + " - [" + string(getLastSocketErrorText(&errId)) +"])";
	return msg;
}

// =====================================================
//	class Ip
// =====================================================

Ip::Ip(){
	bytes[0]= 0;
	bytes[1]= 0;
	bytes[2]= 0;
	bytes[3]= 0;
}

Ip::Ip(unsigned char byte0, unsigned char byte1, unsigned char byte2, unsigned char byte3){
	bytes[0]= byte0;
	bytes[1]= byte1;
	bytes[2]= byte2;
	bytes[3]= byte3;
}


Ip::Ip(const string& ipString){
	size_t offset= 0;
	int byteIndex= 0;

	for(byteIndex= 0; byteIndex<4; ++byteIndex){
		size_t dotPos= ipString.find_first_of('.', offset);

		bytes[byteIndex]= atoi(ipString.substr(offset, dotPos-offset).c_str());
		offset= dotPos+1;
	}
}

string Ip::getString() const{
	return intToStr(bytes[0]) + "." + intToStr(bytes[1]) + "." + intToStr(bytes[2]) + "." + intToStr(bytes[3]);
}

// ===============================================
//	class Socket
// ===============================================

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(BSD) || defined(__APPLE__) || defined(__linux__)
# define USE_GETIFADDRS 1
# include <ifaddrs.h>
#endif

static uint32 SockAddrToUint32(struct in_addr * a) {
	uint32 result = 0;

	//printf("a [%p]\n",a);
	if(a != NULL) {
		result = ntohl(a->s_addr);
	}

	return result;
}

static uint32 SockAddrToUint32(struct sockaddr * a) {
	uint32 result = 0;

	//printf("a [%p] family = %d\n",a,(a ? a->sa_family : -1));
	if(a != NULL && (a->sa_family == AF_INET || a->sa_family == AF_UNSPEC)) {
		//result = SockAddrToUint32((((struct sockaddr_in *)a)->sin_addr.s_addr);
		result = SockAddrToUint32(&((struct sockaddr_in *)a)->sin_addr);
	}

	return result;
}

// convert a numeric IP address into its string representation
void Ip::Inet_NtoA(uint32 addr, char * ipbuf)
{
   sprintf(ipbuf, "%d.%d.%d.%d", (addr>>24)&0xFF, (addr>>16)&0xFF, (addr>>8)&0xFF, (addr>>0)&0xFF);
}

#if defined(WIN32)
// convert a string represenation of an IP address into its numeric equivalent
static uint32 Inet_AtoN(const char * buf)
{
   // net_server inexplicably doesn't have this function; so I'll just fake it
   uint32 ret = 0;
   int shift = 24;  // fill out the MSB first
   bool startQuad = true;
   while((shift >= 0)&&(*buf))
   {
      if (startQuad)
      {
         unsigned char quad = (unsigned char) atoi(buf);
         ret |= (((uint32)quad) << shift);
         shift -= 8;
      }
      startQuad = (*buf == '.');
      buf++;
   }
   return ret;
}
#endif

/*
static void PrintNetworkInterfaceInfos()
{
#if defined(USE_GETIFADDRS)
   // BSD-style implementation
   struct ifaddrs * ifap;
   if (getifaddrs(&ifap) == 0)
   {
      struct ifaddrs * p = ifap;
      while(p)
      {
         uint32 ifaAddr  = SockAddrToUint32(p->ifa_addr);
         uint32 maskAddr = SockAddrToUint32(p->ifa_netmask);
         uint32 dstAddr  = SockAddrToUint32(p->ifa_dstaddr);
         if (ifaAddr > 0)
         {
            char ifaAddrStr[32];  Ip::Inet_NtoA(ifaAddr,  ifaAddrStr);
            char maskAddrStr[32]; Ip::Inet_NtoA(maskAddr, maskAddrStr);
            char dstAddrStr[32];  Ip::Inet_NtoA(dstAddr,  dstAddrStr);
            printf("  Found interface:  name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p->ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
         }
         p = p->ifa_next;
      }
      freeifaddrs(ifap);
   }
#elif defined(WIN32)
   // Windows XP style implementation

   // Adapted from example code at http://msdn2.microsoft.com/en-us/library/aa365917.aspx
   // Now get Windows' IPv4 addresses table.  Once again, we gotta call GetIpAddrTable()
   // multiple times in order to deal with potential race conditions properly.
   MIB_IPADDRTABLE * ipTable = NULL;
   {
      ULONG bufLen = 0;
      for (int i=0; i<5; i++)
      {
         DWORD ipRet = GetIpAddrTable(ipTable, &bufLen, false);
         if (ipRet == ERROR_INSUFFICIENT_BUFFER)
         {
            free(ipTable);  // in case we had previously allocated it
            ipTable = (MIB_IPADDRTABLE *) malloc(bufLen);
         }
         else if (ipRet == NO_ERROR) break;
         else
         {
            free(ipTable);
            ipTable = NULL;
            break;
         }
     }
   }

   if (ipTable)
   {
      // Try to get the Adapters-info table, so we can given useful names to the IP
      // addresses we are returning.  Gotta call GetAdaptersInfo() up to 5 times to handle
      // the potential race condition between the size-query call and the get-data call.
      // I love a well-designed API :^P
      IP_ADAPTER_INFO * pAdapterInfo = NULL;
      {
         ULONG bufLen = 0;
         for (int i=0; i<5; i++)
         {
            DWORD apRet = GetAdaptersInfo(pAdapterInfo, &bufLen);
            if (apRet == ERROR_BUFFER_OVERFLOW)
            {
               free(pAdapterInfo);  // in case we had previously allocated it
               pAdapterInfo = (IP_ADAPTER_INFO *) malloc(bufLen);
            }
            else if (apRet == ERROR_SUCCESS) break;
            else
            {
               free(pAdapterInfo);
               pAdapterInfo = NULL;
               break;
            }
         }
      }

      for (DWORD i=0; i<ipTable->dwNumEntries; i++)
      {
         const MIB_IPADDRROW & row = ipTable->table[i];

         // Now lookup the appropriate adaptor-name in the pAdaptorInfos, if we can find it
         const char * name = NULL;
         const char * desc = NULL;
         if (pAdapterInfo)
         {
            IP_ADAPTER_INFO * next = pAdapterInfo;
            while((next)&&(name==NULL))
            {
               IP_ADDR_STRING * ipAddr = &next->IpAddressList;
               while(ipAddr)
               {
                  if (Inet_AtoN(ipAddr->IpAddress.String) == ntohl(row.dwAddr))
                  {
                     name = next->AdapterName;
                     desc = next->Description;
                     break;
                  }
                  ipAddr = ipAddr->Next;
               }
               next = next->Next;
            }
         }
         char buf[128];
         if (name == NULL)
         {
            snprintf(buf, 128,"unnamed-%i", i);
            name = buf;
         }

         uint32 ipAddr  = ntohl(row.dwAddr);
         uint32 netmask = ntohl(row.dwMask);
         uint32 baddr   = ipAddr & netmask;
         if (row.dwBCastAddr) baddr |= ~netmask;

         char ifaAddrStr[32];  Ip::Inet_NtoA(ipAddr,  ifaAddrStr);
         char maskAddrStr[32]; Ip::Inet_NtoA(netmask, maskAddrStr);
         char dstAddrStr[32];  Ip::Inet_NtoA(baddr,   dstAddrStr);
         printf("  Found interface:  name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", name, desc?desc:"unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
      }

      free(pAdapterInfo);
      free(ipTable);
   }
#else
   // Dunno what we're running on here!
#  error "Don't know how to implement PrintNetworkInterfaceInfos() on this OS!"
#endif
}
*/

string getNetworkInterfaceBroadcastAddress(string ipAddress)
{
	string broadCastAddress = "";

#if defined(USE_GETIFADDRS)
   // BSD-style implementation
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
   struct ifaddrs * ifap;
   if (getifaddrs(&ifap) == 0)
   {
	   if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
      struct ifaddrs * p = ifap;
      while(p)
      {
         uint32 ifaAddr  = SockAddrToUint32(p->ifa_addr);
         uint32 maskAddr = SockAddrToUint32(p->ifa_netmask);
         uint32 dstAddr  = SockAddrToUint32(p->ifa_dstaddr);
         if (ifaAddr > 0)
         {
        	 if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

            char ifaAddrStr[32];  Ip::Inet_NtoA(ifaAddr,  ifaAddrStr);
            char maskAddrStr[32]; Ip::Inet_NtoA(maskAddr, maskAddrStr);
            char dstAddrStr[32];  Ip::Inet_NtoA(dstAddr,  dstAddrStr);
            //printf("  Found interface:  name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p->ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
			if(strcmp(ifaAddrStr,ipAddress.c_str()) == 0) {
				broadCastAddress = dstAddrStr;
			}

			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ifaAddrStr [%s], maskAddrStr [%s], dstAddrStr[%s], ipAddress [%s], broadCastAddress [%s]\n",__FILE__,__FUNCTION__,__LINE__,ifaAddrStr,maskAddrStr,dstAddrStr,ipAddress.c_str(),broadCastAddress.c_str());
         }
         p = p->ifa_next;
      }
      freeifaddrs(ifap);
   }
#elif defined(WIN32)
   // Windows XP style implementation

   // Adapted from example code at http://msdn2.microsoft.com/en-us/library/aa365917.aspx
   // Now get Windows' IPv4 addresses table.  Once again, we gotta call GetIpAddrTable()
   // multiple times in order to deal with potential race conditions properly.
   PMIB_IPADDRTABLE ipTable = NULL;
   // Before calling AddIPAddress we use GetIpAddrTable to get
   // an adapter to which we can add the IP.
   ipTable = (PMIB_IPADDRTABLE) malloc(sizeof (MIB_IPADDRTABLE));
   ipTable->dwNumEntries = 0;

   {
      ULONG bufLen = 0;
      for (int i = 0; i < 5; i++) {

         DWORD ipRet = GetIpAddrTable(ipTable, &bufLen, false);
         if (ipRet == ERROR_INSUFFICIENT_BUFFER) {
            free(ipTable);  // in case we had previously allocated it
            ipTable = (MIB_IPADDRTABLE *) malloc(bufLen);
            ipTable->dwNumEntries = 0;
         }
         else if(ipRet == NO_ERROR) {
        	 break;
         }
         else {
            free(ipTable);
            ipTable = NULL;
            break;
         }
     }
   }

   if (ipTable) {
      // Try to get the Adapters-info table, so we can given useful names to the IP
      // addresses we are returning.  Gotta call GetAdaptersInfo() up to 5 times to handle
      // the potential race condition between the size-query call and the get-data call.
      // I love a well-designed API :^P
      IP_ADAPTER_INFO * pAdapterInfo = NULL;
      {
         ULONG bufLen = 0;
         for (int i = 0; i < 5; i++) {
            DWORD apRet = GetAdaptersInfo(pAdapterInfo, &bufLen);
            if (apRet == ERROR_BUFFER_OVERFLOW) {
               free(pAdapterInfo);  // in case we had previously allocated it
               pAdapterInfo = (IP_ADAPTER_INFO *) malloc(bufLen);
            }
            else if(apRet == ERROR_SUCCESS) {
            	break;
            }
            else {
               free(pAdapterInfo);
               pAdapterInfo = NULL;
               break;
            }
         }
      }

      for (DWORD i = 0; i < ipTable->dwNumEntries; i++) {
         const MIB_IPADDRROW & row = ipTable->table[i];

         // Now lookup the appropriate adaptor-name in the pAdaptorInfos, if we can find it
         const char * name = NULL;
         //const char * desc = NULL;
         if (pAdapterInfo) {
            IP_ADAPTER_INFO * next = pAdapterInfo;
            while((next)&&(name==NULL)) {
               IP_ADDR_STRING * ipAddr = &next->IpAddressList;
               while(ipAddr) {
                  if (Inet_AtoN(ipAddr->IpAddress.String) == ntohl(row.dwAddr)) {
                     name = next->AdapterName;
                     //desc = next->Description;
                     break;
                  }
                  ipAddr = ipAddr->Next;
               }
               next = next->Next;
            }
         }
         if (name == NULL) {
            name = "";
         }

         uint32 ipAddr  = ntohl(row.dwAddr);
         uint32 netmask = ntohl(row.dwMask);
         uint32 baddr   = ipAddr & netmask;
         if (row.dwBCastAddr) {
        	 baddr |= ~netmask;
         }

         char ifaAddrStr[32];
         Ip::Inet_NtoA(ipAddr,  ifaAddrStr);
         char maskAddrStr[32];
         Ip::Inet_NtoA(netmask, maskAddrStr);
         char dstAddrStr[32];
         Ip::Inet_NtoA(baddr,   dstAddrStr);
         //printf("  Found interface:  name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", name, desc?desc:"unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
		 if(strcmp(ifaAddrStr,ipAddress.c_str()) == 0) {
			broadCastAddress = dstAddrStr;
		 }
      }

      if(pAdapterInfo) free(pAdapterInfo);
      if(ipTable) free(ipTable);
   }
#else
   // Dunno what we're running on here!
#  error "Don't know how to implement PrintNetworkInterfaceInfos() on this OS!"
#endif

	return broadCastAddress;
}

uint32 Socket::getConnectedIPAddress(string IP) {
	sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));

    addr.sin_family= AF_INET;
    if(IP == "") {
        IP = connectedIpAddress;
    }
	addr.sin_addr.s_addr= inet_addr(IP.c_str());
	//addr.sin_port= htons(port);

	return SockAddrToUint32((struct sockaddr *)&addr);
}

std::vector<std::string> Socket::getLocalIPAddressList() {
	std::vector<std::string> ipList;

	/* get my host name */
	char myhostname[101]="";
	gethostname(myhostname,100);

	struct hostent* myhostent = gethostbyname(myhostname);
	if(myhostent) {
		// get all host IP addresses (Except for loopback)
		char myhostaddr[101] = "";
		//int ipIdx = 0;
		//while (myhostent->h_addr_list[ipIdx] != 0) {
		for(int ipIdx = 0; myhostent->h_addr_list[ipIdx] != NULL; ++ipIdx) {
			Ip::Inet_NtoA(SockAddrToUint32((struct in_addr *)myhostent->h_addr_list[ipIdx]), myhostaddr);

		   //printf("ipIdx = %d [%s]\n",ipIdx,myhostaddr);
		   if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,myhostaddr);

		   if(strlen(myhostaddr) > 0 &&
			  strncmp(myhostaddr,"127.",4) != 0 &&
			  strncmp(myhostaddr,"0.",2) != 0) {
			   ipList.push_back(myhostaddr);
		   }
		   //ipIdx++;
		}
	}

#ifndef WIN32

	// Now check all linux network devices
	std::vector<string> intfTypes;
	intfTypes.push_back("lo");
	intfTypes.push_back("eth");
	intfTypes.push_back("wlan");
	intfTypes.push_back("vlan");
	intfTypes.push_back("vboxnet");
	intfTypes.push_back("br-lan");
	intfTypes.push_back("br-gest");

	for(int intfIdx = 0; intfIdx < (int)intfTypes.size(); intfIdx++) {
		string intfName = intfTypes[intfIdx];
		for(int idx = 0; idx < 10; ++idx) {
			PLATFORM_SOCKET fd = socket(AF_INET, SOCK_DGRAM, 0);
			//PLATFORM_SOCKET fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

			/* I want to get an IPv4 IP address */
			struct ifreq ifr;
			struct ifreq ifrA;
			ifr.ifr_addr.sa_family = AF_INET;
			ifrA.ifr_addr.sa_family = AF_INET;

			/* I want IP address attached to "eth0" */
			char szBuf[100]="";
			snprintf(szBuf,100,"%s%d",intfName.c_str(),idx);
			int maxIfNameLength = std::min((int)strlen(szBuf),IFNAMSIZ-1);

			strncpy(ifr.ifr_name, szBuf, maxIfNameLength);
			ifr.ifr_name[maxIfNameLength] = '\0';
			strncpy(ifrA.ifr_name, szBuf, maxIfNameLength);
			ifrA.ifr_name[maxIfNameLength] = '\0';

			int result_ifaddrr = ioctl(fd, SIOCGIFADDR, &ifr);
			ioctl(fd, SIOCGIFFLAGS, &ifrA);
			if(fd >= 0) close(fd);

			if(result_ifaddrr >= 0) {
				struct sockaddr_in *pSockAddr = (struct sockaddr_in *)&ifr.ifr_addr;
				if(pSockAddr != NULL) {

					char myhostaddr[101] = "";
					Ip::Inet_NtoA(SockAddrToUint32(&pSockAddr->sin_addr), myhostaddr);
					if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] szBuf [%s], myhostaddr = [%s], ifr.ifr_flags = %d, ifrA.ifr_flags = %d, ifr.ifr_name [%s]\n",__FILE__,__FUNCTION__,__LINE__,szBuf,myhostaddr,ifr.ifr_flags,ifrA.ifr_flags,ifr.ifr_name);

					// Now only include interfaces that are both UP and running
					if( (ifrA.ifr_flags & IFF_UP) 		== IFF_UP &&
						(ifrA.ifr_flags & IFF_RUNNING) 	== IFF_RUNNING) {
						if( strlen(myhostaddr) > 0 &&
							strncmp(myhostaddr,"127.",4) != 0  &&
							strncmp(myhostaddr,"0.",2) != 0) {
							if(std::find(ipList.begin(),ipList.end(),myhostaddr) == ipList.end()) {
								ipList.push_back(myhostaddr);
							}
						}
					}
				}
			}
		}
	}

#endif

	return ipList;
}

bool Socket::isSocketValid() const {
	return Socket::isSocketValid(&sock);
}

bool Socket::isSocketValid(const PLATFORM_SOCKET *validateSocket) {
#ifdef WIN32
	if(validateSocket == NULL || (*validateSocket) == 0) {
		return false;
	}
	else {
		return (*validateSocket != INVALID_SOCKET);
	}
#else
	if(validateSocket == NULL) {
		return false;
	}
	else {
		return (*validateSocket > 0);
	}
#endif
}

Socket::Socket(PLATFORM_SOCKET sock) {
	dataSynchAccessorRead = new Mutex(CODE_AT_LINE);
	dataSynchAccessorWrite = new Mutex(CODE_AT_LINE);
	inSocketDestructorSynchAccessor = new Mutex(CODE_AT_LINE);
	lastSocketError = 0;

	MutexSafeWrapper safeMutexSocketDestructorFlag(inSocketDestructorSynchAccessor,CODE_AT_LINE);
	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);
	this->inSocketDestructor = false;
	lastThreadedPing = 0;
	lastDebugEvent = 0;
	//safeMutexSocketDestructorFlag.ReleaseLock();

	//this->pingThread = NULL;
	//pingThreadAccessor.setOwnerId(CODE_AT_LINE);
	dataSynchAccessorRead->setOwnerId(CODE_AT_LINE);
	dataSynchAccessorWrite->setOwnerId(CODE_AT_LINE);

	this->sock= sock;
	this->isSocketBlocking = true;
	this->connectedIpAddress = "";
}

Socket::Socket() {
	dataSynchAccessorRead = new Mutex(CODE_AT_LINE);
	dataSynchAccessorWrite = new Mutex(CODE_AT_LINE);
	inSocketDestructorSynchAccessor = new Mutex(CODE_AT_LINE);
	lastSocketError = 0;
	lastDebugEvent = 0;
	lastThreadedPing = 0;

	MutexSafeWrapper safeMutexSocketDestructorFlag(inSocketDestructorSynchAccessor,CODE_AT_LINE);
	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);
	this->inSocketDestructor = false;
	//safeMutexSocketDestructorFlag.ReleaseLock();

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	//this->pingThread = NULL;

	this->connectedIpAddress = "";

	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(isSocketValid() == false) {
		throwException("Error creating socket");
	}

	this->isSocketBlocking = true;

#ifdef __APPLE__
    int set = 1;
    setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
#endif

    /* Disable the Nagle (TCP No Delay) algorithm */
    if(Socket::disableNagle == true) {
		int flag = 1;
		int ret = setsockopt( sock, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag) );
		if (ret == -1) {
		  printf("Couldn't setsockopt(TCP_NODELAY)\n");
		}
    }

	if(Socket::DEFAULT_SOCKET_SENDBUF_SIZE >= 0) {
		int bufsize 	 = 0;
		socklen_t optlen = sizeof(bufsize);

		int ret = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, &optlen);
		printf("Original setsockopt(SO_SNDBUF) = [%d] new will be [%d] ret = %d\n",bufsize,Socket::DEFAULT_SOCKET_SENDBUF_SIZE,ret);

		ret = setsockopt( sock, SOL_SOCKET, SO_SNDBUF, (char *) &Socket::DEFAULT_SOCKET_SENDBUF_SIZE, sizeof( int ) );
		if (ret == -1) {
		  printf("Couldn't setsockopt(SO_SNDBUF) [%d]\n",Socket::DEFAULT_SOCKET_SENDBUF_SIZE);
		}
	}

	if(Socket::DEFAULT_SOCKET_RECVBUF_SIZE >= 0) {
		int bufsize 	 = 0;
		socklen_t optlen = sizeof(bufsize);

		int ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize, &optlen);
		printf("Original setsockopt(SO_RCVBUF) = [%d] new will be [%d] ret = %d\n",bufsize,Socket::DEFAULT_SOCKET_RECVBUF_SIZE,ret);

		ret = setsockopt( sock, SOL_SOCKET, SO_RCVBUF, (char *) &Socket::DEFAULT_SOCKET_RECVBUF_SIZE, sizeof( int ) );
		if (ret == -1) {
		  printf("Couldn't setsockopt(SO_RCVBUF) [%d]\n",Socket::DEFAULT_SOCKET_RECVBUF_SIZE);
		}
	}

}

float Socket::getThreadedPingMS(std::string host) {
	float result = -1;
/*
	if(pingThread == NULL) {
		lastThreadedPing = 0;
		pingThread = new SimpleTaskThread(this,0,50);
		pingThread->setUniqueID(__FILE__ + "_" + __FUNCTION);
		pingThread->start();
	}

	if(pingCache.find(host) == pingCache.end()) {
		MutexSafeWrapper safeMutex(&pingThreadAccessor);
		safeMutex.ReleaseLock();
		result = getAveragePingMS(host, 1);
		pingCache[host]=result;
	}
	else {
		MutexSafeWrapper safeMutex(&pingThreadAccessor);
		result = pingCache[host];
		safeMutex.ReleaseLock();
	}
*/
	return result;
}

/*
void Socket::simpleTask(BaseThread *callingThread)  {
	// update ping times every x seconds
	const int pingFrequencySeconds = 2;
	if(difftime(time(NULL),lastThreadedPing) < pingFrequencySeconds) {
		return;
	}
	lastThreadedPing = time(NULL);

	//printf("Pinging hosts...\n");

	for(std::map<string,double>::iterator iterMap = pingCache.begin();
		iterMap != pingCache.end(); iterMap++) {
		MutexSafeWrapper safeMutex(&pingThreadAccessor,CODE_AT_LINE);
		iterMap->second = getAveragePingMS(iterMap->first, 1);
		safeMutex.ReleaseLock();
	}
}
*/

Socket::~Socket() {
	MutexSafeWrapper safeMutexSocketDestructorFlag(inSocketDestructorSynchAccessor,CODE_AT_LINE);
	if(this->inSocketDestructor == true) {
		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] this->inSocketDestructor == true\n",__FILE__,__FUNCTION__,__LINE__);
		return;
	}
	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);
	this->inSocketDestructor = true;

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START closing socket = %d...\n",__FILE__,__FUNCTION__,sock);

	//safeMutexSocketDestructorFlag.ReleaseLock();

    disconnectSocket();

    // Allow other callers with a lock on the mutexes to let them go
	for(time_t elapsed = time(NULL);
		(dataSynchAccessorRead->getRefCount() > 0 ||
		 dataSynchAccessorWrite->getRefCount() > 0) &&
		 difftime((long int)time(NULL),elapsed) <= 2;) {
		printf("Waiting in socket destructor\n");
		//sleep(0);
	}

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END closing socket = %d...\n",__FILE__,__FUNCTION__,sock);

	//delete pingThread;
	//pingThread = NULL;
	safeMutexSocketDestructorFlag.ReleaseLock();

	delete dataSynchAccessorRead;
	dataSynchAccessorRead = NULL;
	delete dataSynchAccessorWrite;
	dataSynchAccessorWrite = NULL;
	delete inSocketDestructorSynchAccessor;
	inSocketDestructorSynchAccessor = NULL;
}

void Socket::disconnectSocket() {
	//printf("Socket disconnecting sock = %d\n",sock);

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START closing socket = %d...\n",__FILE__,__FUNCTION__,sock);

    if(isSocketValid() == true) {
    	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] calling shutdown and close for socket = %d...\n",__FILE__,__FUNCTION__,sock);

        MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
        MutexSafeWrapper safeMutex1(dataSynchAccessorWrite,CODE_AT_LINE);

        if(isSocketValid() == true) {
        ::shutdown(sock,2);
#ifndef WIN32
        ::close(sock);
        sock = -1;
#else
        ::closesocket(sock);
        sock = INVALID_SOCKET;
#endif
        }
        safeMutex.ReleaseLock();
        safeMutex1.ReleaseLock();
    }

    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END closing socket = %d...\n",__FILE__,__FUNCTION__,sock);
}

// Int lookup is socket fd while bool result is whether or not that socket was signalled for reading
bool Socket::hasDataToRead(std::map<PLATFORM_SOCKET,bool> &socketTriggeredList)
{
    bool bResult = false;

    if(socketTriggeredList.empty() == false) {
        /* Watch stdin (fd 0) to see when it has input. */
        fd_set rfds;
        FD_ZERO(&rfds);

		string socketDebugList = "";
        PLATFORM_SOCKET imaxsocket = 0;
        for(std::map<PLATFORM_SOCKET,bool>::iterator itermap = socketTriggeredList.begin();
            itermap != socketTriggeredList.end(); ++itermap) {
        	PLATFORM_SOCKET socket = itermap->first;
            if(Socket::isSocketValid(&socket) == true) {
                FD_SET(socket, &rfds);
                imaxsocket = max(socket,imaxsocket);

				if(socketDebugList != "") {
					socketDebugList += ",";
				}
				socketDebugList += intToStr(socket);
            }
        }

        if(imaxsocket > 0) {
            /* Wait up to 0 seconds. */
            struct timeval tv;
            tv.tv_sec = 0;
            tv.tv_usec = 0;


            int retval = 0;
            {
            	//MutexSafeWrapper safeMutex(&dataSynchAccessor);
            	retval = select((int)imaxsocket + 1, &rfds, NULL, NULL, &tv);
            }
            if(retval < 0) {
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d, ERROR SELECTING SOCKET DATA retval = %d error = %s, socketDebugList [%s]\n",__FILE__,__FUNCTION__,__LINE__,retval,getLastSocketErrorFormattedText().c_str(),socketDebugList.c_str());
				printf("In [%s::%s] Line: %d, ERROR SELECTING SOCKET DATA retval = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,retval,getLastSocketErrorFormattedText().c_str());
            }
            else if(retval) {
                bResult = true;

                for(std::map<PLATFORM_SOCKET,bool>::iterator itermap = socketTriggeredList.begin();
                    itermap != socketTriggeredList.end(); ++itermap) {
                	PLATFORM_SOCKET socket = itermap->first;
                    if (FD_ISSET(socket, &rfds)) {
                        itermap->second = true;
                    }
                    else {
                        itermap->second = false;
                    }
                }
            }
        }
    }

    return bResult;
}

bool Socket::hasDataToRead()
{
	MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
    return Socket::hasDataToRead(sock) ;
}

bool Socket::hasDataToRead(PLATFORM_SOCKET socket)
{
    bool bResult = false;

    if(Socket::isSocketValid(&socket) == true)
    {
        fd_set rfds;
        struct timeval tv;

        /* Watch stdin (fd 0) to see when it has input. */
        FD_ZERO(&rfds);
        FD_SET(socket, &rfds);

        /* Wait up to 0 seconds. */
        tv.tv_sec = 0;
        tv.tv_usec = 0;

        int retval = 0;
        {
        	//MutexSafeWrapper safeMutex(&dataSynchAccessor);
        	retval = select((int)socket + 1, &rfds, NULL, NULL, &tv);
        }
        if(retval < 0) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d, ERROR SELECTING SOCKET DATA retval = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,retval,getLastSocketErrorFormattedText().c_str());
			printf("In [%s::%s] Line: %d, ERROR SELECTING SOCKET DATA retval = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,retval,getLastSocketErrorFormattedText().c_str());
        }
        else if(retval)
        {
            if (FD_ISSET(socket, &rfds))
            {
                bResult = true;
            }
        }
    }

    return bResult;
}

bool Socket::hasDataToReadWithWait(int waitMicroseconds) {
	MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
    return Socket::hasDataToReadWithWait(sock,waitMicroseconds) ;
}

bool Socket::hasDataToReadWithWait(PLATFORM_SOCKET socket,int waitMicroseconds) {
    bool bResult = false;

    Chrono chono;
    chono.start();
    if(Socket::isSocketValid(&socket) == true)
    {
        fd_set rfds;
        struct timeval tv;

        /* Watch stdin (fd 0) to see when it has input. */
        FD_ZERO(&rfds);
        FD_SET(socket, &rfds);

        /* Wait up to 0 seconds. */
        tv.tv_sec = 0;
        tv.tv_usec = waitMicroseconds;

        int retval = 0;
        {
        	//MutexSafeWrapper safeMutex(&dataSynchAccessor);
        	retval = select((int)socket + 1, &rfds, NULL, NULL, &tv);
        }
		if(retval < 0) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d, ERROR SELECTING SOCKET DATA retval = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,retval,getLastSocketErrorFormattedText().c_str());
			printf("In [%s::%s] Line: %d, ERROR SELECTING SOCKET DATA retval = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,retval,getLastSocketErrorFormattedText().c_str());
		}

        if(retval)
        {
            if (FD_ISSET(socket, &rfds))
            {
                bResult = true;
            }
        }
    }

    //printf("hasdata waited [%d] milliseconds [%d], bResult = %d\n",chono.getMillis(),waitMilliseconds,bResult);
    return bResult;
}

int Socket::getDataToRead(bool wantImmediateReply) {
	unsigned long size = 0;

    //fd_set rfds;
    //struct timeval tv;
    //int retval;

    /* Watch stdin (fd 0) to see when it has input. */
    //FD_ZERO(&rfds);
    //FD_SET(sock, &rfds);

    /* Wait up to 0 seconds. */
    //tv.tv_sec = 0;
    //tv.tv_usec = 0;

    //retval = select(sock + 1, &rfds, NULL, NULL, &tv);
    //if(retval)
    if(isSocketValid() == true)
    {
    	//int loopCount = 1;
    	for(time_t elapsed = time(NULL); difftime((long int)time(NULL),elapsed) < 1;) {
			/* ioctl isn't posix, but the following seems to work on all modern
			 * unixes */
	#ifndef WIN32
			int err = ioctl(sock, FIONREAD, &size);
	#else
			int err= ioctlsocket(sock, FIONREAD, &size);
	#endif
			int lastSocketError = getLastSocketError();
			if(err < 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN)
			{
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR PEEKING SOCKET DATA, err = %d %s\n",__FILE__,__FUNCTION__,__LINE__,err,getLastSocketErrorFormattedText().c_str());
				break;
			}
			else if(err == 0)
			{
				if(isConnected() == false) {
					if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR PEEKING SOCKET DATA, err = %d %s\n",__FILE__,__FUNCTION__,__LINE__,err,getLastSocketErrorFormattedText().c_str());
					break;
				}
			}

			if(size > 0) {
				break;
			}
			else if(hasDataToRead() == true) {
				//if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING PEEKING SOCKET DATA, (hasDataToRead() == true) err = %d, sock = %d, size = " MG_SIZE_T_SPECIFIER ", loopCount = %d\n",__FILE__,__FUNCTION__,__LINE__,err,sock,size,loopCount);
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING PEEKING SOCKET DATA, (hasDataToRead() == true) err = %d, sock = %d, size = " MG_SIZE_T_SPECIFIER "\n",__FILE__,__FUNCTION__,__LINE__,err,sock,size);
			}
			else {
				//if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING PEEKING SOCKET DATA, err = %d, sock = %d, size = " MG_SIZE_T_SPECIFIER ", loopCount = %d\n",__FILE__,__FUNCTION__,__LINE__,err,sock,size,loopCount);
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING PEEKING SOCKET DATA, err = %d, sock = %d, size = " MG_SIZE_T_SPECIFIER "\n",__FILE__,__FUNCTION__,__LINE__,err,sock,size);
				break;
			}

			if(wantImmediateReply == true) {
				break;
			}

			//loopCount++;
    	}
    }

	return static_cast<int>(size);
}

int Socket::send(const void *data, int dataSize) {
	const int MAX_SEND_WAIT_SECONDS = 3;

	int bytesSent= 0;
	if(isSocketValid() == true)	{
		errno = 0;

//    	MutexSafeWrapper safeMutexSocketDestructorFlag(&inSocketDestructorSynchAccessor,CODE_AT_LINE);
//    	if(this->inSocketDestructor == true) {
//    		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] this->inSocketDestructor == true\n",__FILE__,__FUNCTION__,__LINE__);
//    		return -1;
//    	}
//    	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);
//    	safeMutexSocketDestructorFlag.ReleaseLock();

		MutexSafeWrapper safeMutex(dataSynchAccessorWrite,CODE_AT_LINE);

		if(isSocketValid() == true)	{
#ifdef __APPLE__
        bytesSent = ::send(sock, (const char *)data, dataSize, SO_NOSIGPIPE);
#else
        bytesSent = ::send(sock, (const char *)data, dataSize, MSG_NOSIGNAL | MSG_DONTWAIT);
#endif
		}
        safeMutex.ReleaseLock();
	}

	// TEST errors
	//bytesSent = -1;
	// END TEST

	int lastSocketError = getLastSocketError();
	if(bytesSent < 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR WRITING SOCKET DATA, err = %d error = %s dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesSent,getLastSocketErrorFormattedText(&lastSocketError).c_str(),dataSize);
	}
	else if(bytesSent < 0 && lastSocketError == PLATFORM_SOCKET_TRY_AGAIN && isConnected() == true) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #1 EAGAIN during send, trying again... dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,dataSize);

		int attemptCount = 0;
	    time_t tStartTimer = time(NULL);
	    while((bytesSent < 0 && lastSocketError == PLATFORM_SOCKET_TRY_AGAIN) &&
	    		(difftime((long int)time(NULL),tStartTimer) <= MAX_SEND_WAIT_SECONDS)) {
	    	attemptCount++;
	    	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] attemptCount = %d\n",__FILE__,__FUNCTION__,__LINE__,attemptCount);

	    	MutexSafeWrapper safeMutex(dataSynchAccessorWrite,CODE_AT_LINE);
            if(isConnected() == true) {
            	struct timeval timeVal;
            	timeVal.tv_sec = 1;
            	timeVal.tv_usec = 0;
            	bool canWrite = isWritable(&timeVal);

            	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] attemptCount = %d, sock = %d, dataSize = %d, data = %p, canWrite = %d\n",__FILE__,__FUNCTION__,__LINE__,attemptCount,sock,dataSize,data,canWrite);

//	        	MutexSafeWrapper safeMutexSocketDestructorFlag(&inSocketDestructorSynchAccessor,CODE_AT_LINE);
//	        	if(this->inSocketDestructor == true) {
//	        		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] this->inSocketDestructor == true\n",__FILE__,__FUNCTION__,__LINE__);
//	        		return -1;
//	        	}
//	        	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);
//	        	safeMutexSocketDestructorFlag.ReleaseLock();


#ifdef __APPLE__
                bytesSent = ::send(sock, (const char *)data, dataSize, SO_NOSIGPIPE);
#else
                bytesSent = ::send(sock, (const char *)data, dataSize, MSG_NOSIGNAL | MSG_DONTWAIT);
#endif
				lastSocketError = getLastSocketError();
                if(bytesSent < 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) {
                    break;
                }

                //safeMutex.ReleaseLock();

                if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #2 EAGAIN during send, trying again returned: %d\n",__FILE__,__FUNCTION__,__LINE__,bytesSent);
	        }
	        else {
                int iErr = getLastSocketError();

                safeMutex.ReleaseLock();
                disconnectSocket();

                if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] DISCONNECTED SOCKET error while sending socket data, bytesSent = %d, error = %s\n",__FILE__,__FUNCTION__,bytesSent,getLastSocketErrorFormattedText(&iErr).c_str());
                break;

	        }
            if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] attemptCount = %d\n",__FILE__,__FUNCTION__,__LINE__,attemptCount);
	    }
	}

	if(isConnected() == true && bytesSent > 0 && bytesSent < dataSize) {
		lastSocketError = getLastSocketError();
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] need to send more data, trying again getLastSocketError() = %d, bytesSent = %d, dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,lastSocketError,bytesSent,dataSize);

		int totalBytesSent = bytesSent;
		int attemptCount = 0;

		
	    time_t tStartTimer = time(NULL);
	    while(((bytesSent > 0 && totalBytesSent < dataSize) ||
	    		(bytesSent < 0 && lastSocketError == PLATFORM_SOCKET_TRY_AGAIN)) &&
	    		(difftime((long int)time(NULL),tStartTimer) <= MAX_SEND_WAIT_SECONDS)) {
	    	attemptCount++;
	    	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] attemptCount = %d, totalBytesSent = %d\n",__FILE__,__FUNCTION__,__LINE__,attemptCount,totalBytesSent);

	    	MutexSafeWrapper safeMutex(dataSynchAccessorWrite,CODE_AT_LINE);
            if(isConnected() == true) {
            	struct timeval timeVal;
            	timeVal.tv_sec = 1;
            	timeVal.tv_usec = 0;
            	bool canWrite = isWritable(&timeVal);

            	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] attemptCount = %d, sock = %d, dataSize = %d, data = %p, canWrite = %d\n",__FILE__,__FUNCTION__,__LINE__,attemptCount,sock,dataSize,data,canWrite);

//	        	MutexSafeWrapper safeMutexSocketDestructorFlag(&inSocketDestructorSynchAccessor,CODE_AT_LINE);
//	        	if(this->inSocketDestructor == true) {
//	        		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] this->inSocketDestructor == true\n",__FILE__,__FUNCTION__,__LINE__);
//	        		return -1;
//	        	}
//	        	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);
//	        	safeMutexSocketDestructorFlag.ReleaseLock();


	        	const char *sendBuf = (const char *)data;
#ifdef __APPLE__
			    bytesSent = ::send(sock, &sendBuf[totalBytesSent], dataSize - totalBytesSent, SO_NOSIGPIPE);
#else
			    bytesSent = ::send(sock, &sendBuf[totalBytesSent], dataSize - totalBytesSent, MSG_NOSIGNAL | MSG_DONTWAIT);
#endif
				lastSocketError = getLastSocketError();
                if(bytesSent > 0) {
                	totalBytesSent += bytesSent;
                }

                if(bytesSent < 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) {
                    break;
                }

			    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] retry send returned: %d\n",__FILE__,__FUNCTION__,__LINE__,bytesSent);
	        }
	        else {
                int iErr = getLastSocketError();

                safeMutex.ReleaseLock();
                disconnectSocket();

                if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTED SOCKET error while sending socket data, bytesSent = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,bytesSent,getLastSocketErrorFormattedText(&iErr).c_str());
	            break;
	        }

            if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] attemptCount = %d\n",__FILE__,__FUNCTION__,__LINE__,attemptCount);
	    }

	    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] bytesSent = %d, totalBytesSent = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesSent,totalBytesSent);

	    if(bytesSent > 0) {
	    	bytesSent = totalBytesSent;
	    }
	}

	if(bytesSent <= 0 || isConnected() == false) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR WRITING SOCKET DATA, err = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,bytesSent,getLastSocketErrorFormattedText().c_str());

	    int iErr = getLastSocketError();
	    disconnectSocket();

	    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTED SOCKET error while sending socket data, bytesSent = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,bytesSent,getLastSocketErrorFormattedText(&iErr).c_str());
	}

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sock = %d, bytesSent = %d\n",__FILE__,__FUNCTION__,__LINE__,sock,bytesSent);

	return static_cast<int>(bytesSent);
}

int Socket::receive(void *data, int dataSize, bool tryReceiveUntilDataSizeMet) {
	ssize_t bytesReceived = 0;

	if(isSocketValid() == true)	{
		MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
		if(isSocketValid() == true)	{
			bytesReceived = recv(sock, reinterpret_cast<char*>(data), dataSize, 0);
		}
	    safeMutex.ReleaseLock();
	}

	int lastSocketError = getLastSocketError();
	if(bytesReceived < 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] ERROR READING SOCKET DATA error while sending socket data, bytesSent = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived,getLastSocketErrorFormattedText(&lastSocketError).c_str());
	}
	else if(bytesReceived < 0 && lastSocketError == PLATFORM_SOCKET_TRY_AGAIN)	{
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #1 EAGAIN during receive, trying again...\n",__FILE__,__FUNCTION__,__LINE__);

		Chrono chronoElapsed(true);
		const int MAX_RECV_WAIT_SECONDS = 3;
	    time_t tStartTimer = time(NULL);
	    while((bytesReceived < 0 && lastSocketError == PLATFORM_SOCKET_TRY_AGAIN) &&
	    		(difftime((long int)time(NULL),tStartTimer) <= MAX_RECV_WAIT_SECONDS)) {
	        if(isConnected() == false) {
	        	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] Socket is NOT connected!\n",__FILE__,__FUNCTION__,__LINE__);

	        	int iErr = getLastSocketError();
                disconnectSocket();

                if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTED SOCKET error while receiving socket data, bytesSent = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived,getLastSocketErrorFormattedText(&iErr).c_str());
	            break;
	        }
	        //else if(Socket::isReadable(true) == true) {
	        else {
				if(Socket::isReadable(true) == true) {
					MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);

					//SafeSocketBlockToggleWrapper safeBlock(this, true);
					errno = 0;
					bytesReceived = recv(sock, reinterpret_cast<char*>(data), dataSize, 0);
					lastSocketError = getLastSocketError();
					//safeBlock.Restore();
					safeMutex.ReleaseLock();

					if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #2 EAGAIN during receive, trying again returned: %d, lastSocketError = %d, dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived,lastSocketError,(int)dataSize);
					//printf("In [%s::%s Line: %d] #2 EAGAIN during receive, trying again returned: %d, lastSocketError = %d, dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived,lastSocketError,(int)dataSize);
				}
				else {
					//if(chronoElapsed.getMillis() % 3 == 0) {
					//	sleep(1);
					//}
					//else {
					sleep(0);
					//}
				}
	        }
	    }
	}

	if(bytesReceived <= 0) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] bytesReceived = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived);

	    int iErr = getLastSocketError();
	    disconnectSocket();

	    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTED SOCKET error while receiving socket data, bytesReceived = %d, error = %s, dataSize = %d, tryReceiveUntilDataSizeMet = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived,getLastSocketErrorFormattedText(&iErr).c_str(),dataSize,tryReceiveUntilDataSizeMet);
	}
	else if(tryReceiveUntilDataSizeMet == true && bytesReceived < dataSize) {
		int newBufferSize = (dataSize - (int)bytesReceived);
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING, attempting to receive MORE data, bytesReceived = %d, dataSize = %d, newBufferSize = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived,dataSize,newBufferSize);
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s::%s Line: %d] WARNING, attempting to receive MORE data, bytesReceived = %d, dataSize = %d, newBufferSize = %d\n",__FILE__,__FUNCTION__,__LINE__,(int)bytesReceived,dataSize,newBufferSize);

		char *dataAsCharPointer = reinterpret_cast<char *>(data);
		int additionalBytes = receive(&dataAsCharPointer[bytesReceived], newBufferSize, tryReceiveUntilDataSizeMet);

		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING, additionalBytes = %d\n",__FILE__,__FUNCTION__,__LINE__,additionalBytes);
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s::%s Line: %d] WARNING, additionalBytes = %d\n",__FILE__,__FUNCTION__,__LINE__,additionalBytes);

		if(additionalBytes > 0) {
			bytesReceived += additionalBytes;
		}
		else {
			//throw megaglest_runtime_error("additionalBytes == " + intToStr(additionalBytes));
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] additionalBytes == %d\n",__FILE__,__FUNCTION__,__LINE__,additionalBytes);
			if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s::%s Line: %d] additionalBytes == %d\n",__FILE__,__FUNCTION__,__LINE__,additionalBytes);
			if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] additionalBytes == %d\n",__FILE__,__FUNCTION__,__LINE__,additionalBytes);

		    int iErr = getLastSocketError();
		    disconnectSocket();

		    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTED SOCKET error while receiving socket data, bytesReceived = %d, error = %s, dataSize = %d, tryReceiveUntilDataSizeMet = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived,getLastSocketErrorFormattedText(&iErr).c_str(),dataSize,tryReceiveUntilDataSizeMet);
		}
	}
	return static_cast<int>(bytesReceived);
}

SafeSocketBlockToggleWrapper::SafeSocketBlockToggleWrapper(Socket *socket, bool toggle) {
	this->socket = socket;

	if(this->socket != NULL) {
		this->originallyBlocked = socket->getBlock();
		this->newBlocked = toggle;

		if(this->originallyBlocked != this->newBlocked) {
			socket->setBlock(this->newBlocked);
		}
	}
	else {
		this->originallyBlocked = false;
		this->newBlocked = false;
	}
}

void SafeSocketBlockToggleWrapper::Restore() {
	if(this->socket != NULL) {
		if(this->originallyBlocked != this->newBlocked) {
			socket->setBlock(this->originallyBlocked);
			this->newBlocked = this->originallyBlocked;
		}
	}
}

SafeSocketBlockToggleWrapper::~SafeSocketBlockToggleWrapper() {
	Restore();
}

int Socket::peek(void *data, int dataSize,bool mustGetData,int *pLastSocketError) {
	Chrono chrono;
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) chrono.start();

    int lastSocketError = 0;
	int err = 0;
	if(isSocketValid() == true) {
		//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());

//    	MutexSafeWrapper safeMutexSocketDestructorFlag(&inSocketDestructorSynchAccessor,CODE_AT_LINE);
//    	if(this->inSocketDestructor == true) {
//    		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] this->inSocketDestructor == true\n",__FILE__,__FUNCTION__,__LINE__);
//    		return -1;
//    	}
//    	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);
//    	safeMutexSocketDestructorFlag.ReleaseLock();

		//MutexSafeWrapper safeMutex(&dataSynchAccessor,CODE_AT_LINE + "_" + intToStr(sock) + "_" + intToStr(dataSize));


		//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
		if(isSocketValid() == true)	{
			MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
//			Chrono recvTimer(true);
			SafeSocketBlockToggleWrapper safeUnblock(this, false);
			errno = 0;
			err = recv(sock, reinterpret_cast<char*>(data), dataSize, MSG_PEEK);
			lastSocketError = getLastSocketError();
			if(pLastSocketError != NULL) {
				*pLastSocketError = lastSocketError;
			}
			safeUnblock.Restore();

//			if(recvTimer.getMillis() > 1000 || (err <= 0 && lastSocketError != 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN)) {
//				printf("#1 PEEK err = %d lastSocketError = %d ms: %lld\n",err,lastSocketError,(long long int)recvTimer.getMillis());

			//if(err != dataSize) {
			//	printf("#1 PEEK err = %d lastSocketError = %d\n",err,lastSocketError);
			//}

//			}
			safeMutex.ReleaseLock();
		}
	    //safeMutex.ReleaseLock();

	    //printf("Peek #1 err = %d\n",err);

	    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) if(chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
	}
	else {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] SOCKET appears to be invalid [%d]\n",__FILE__,__FUNCTION__,__LINE__,sock);
	}
	//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());

	if(err < 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] ERROR PEEKING SOCKET DATA error while sending socket data, err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,err,getLastSocketErrorFormattedText().c_str());
		disconnectSocket();
	}
	else if(err < 0 && lastSocketError == PLATFORM_SOCKET_TRY_AGAIN && mustGetData == true) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #1 ERROR EAGAIN during peek, trying again...\n",__FILE__,__FUNCTION__,__LINE__);

		//printf("Peek #2 err = %d\n",err);

		Chrono chronoElapsed(true);
		const int MAX_PEEK_WAIT_SECONDS = 3;
	    time_t tStartTimer = time(NULL);
	    while((err < 0 && lastSocketError == PLATFORM_SOCKET_TRY_AGAIN) &&
	    		isSocketValid() == true &&
	    		(difftime((long int)time(NULL),tStartTimer) <= MAX_PEEK_WAIT_SECONDS)) {
/*
	        if(isConnected() == false) {
                int iErr = getLastSocketError();
                disconnectSocket();
	            break;
	        }
*/
	        if(Socket::isReadable(true) == true || chronoElapsed.getMillis() % 100 == 0) {

//	        	MutexSafeWrapper safeMutexSocketDestructorFlag(&inSocketDestructorSynchAccessor,CODE_AT_LINE);
//	        	if(this->inSocketDestructor == true) {
//	        		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] this->inSocketDestructor == true\n",__FILE__,__FUNCTION__,__LINE__);
//	        		return -1;
//	        	}
//	        	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);
//	        	safeMutexSocketDestructorFlag.ReleaseLock();

	        	//MutexSafeWrapper safeMutex(&dataSynchAccessor,CODE_AT_LINE + "_" + intToStr(sock) + "_" + intToStr(dataSize));
	        	MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);

//	        	Chrono recvTimer(true);
	        	SafeSocketBlockToggleWrapper safeUnblock(this, false);
	        	errno = 0;
                err = recv(sock, reinterpret_cast<char*>(data), dataSize, MSG_PEEK);
				lastSocketError = getLastSocketError();
				if(pLastSocketError != NULL) {
					*pLastSocketError = lastSocketError;
				}
				safeUnblock.Restore();

//                if(recvTimer.getMillis() > 1000 || (err <= 0 && lastSocketError != 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN)) {
//    				printf("#2 PEEK err = %d lastSocketError = %d ms: %lld\n",err,lastSocketError,(long long int)recvTimer.getMillis());
//    			}

				//printf("Socket peek delayed checking for sock = %d err = %d\n",sock,err);

                safeMutex.ReleaseLock();

                if(err == 0 || err == PLATFORM_SOCKET_TRY_AGAIN) {
                	sleep(0);
                }
                if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) if(chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
                if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #2 EAGAIN during peek, trying again returned: %d\n",__FILE__,__FUNCTION__,__LINE__,err);
	        }
	        //else {
	        	//printf("Socket peek delayed [NOT READABLE] checking for sock = %d err = %d\n",sock,err);
	        //}
	    }

	    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) if(chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
	}
	else if (err == 0) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #2 SOCKET appears to be invalid [%d] lastSocketError [%d] err [%d] mustGetData [%d] dataSize [%d]\n",__FILE__,__FUNCTION__,__LINE__,sock,lastSocketError,err,mustGetData,dataSize);
	}

	//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());

	if(err < 0 || (err == 0 && dataSize != 0) ||
		((err == 0 || err == -1) && dataSize == 0 && lastSocketError != 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN)) {
		//printf("** #1 Socket peek error for sock = %d err = %d lastSocketError = %d\n",sock,err,lastSocketError);

		int iErr = lastSocketError;
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTING SOCKET for socket [%d], err = %d, dataSize = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,sock,err,dataSize,getLastSocketErrorFormattedText(&iErr).c_str());
		//printf("Peek #3 err = %d\n",err);
		//lastSocketError = getLastSocketError();
		if(mustGetData == true || lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) {
			printf("** #2 Socket peek error for sock = %d err = %d lastSocketError = %d mustGetData = %d\n",sock,err,lastSocketError,mustGetData);
			if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTING SOCKET for socket [%d], err = %d, dataSize = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,sock,err,dataSize,getLastSocketErrorFormattedText(&iErr).c_str());

			if(err == 0) {
				printf("** LAST CHANCE for disconnection check for sock = %d\n",sock);
				if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"** LAST CHANCE for disconnection check for sock = %d\n",sock);

				MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
				SafeSocketBlockToggleWrapper safeUnblock(this, false);
				errno = 0;
				int second_err = recv(sock, reinterpret_cast<char*>(data), dataSize, MSG_PEEK);
				safeUnblock.Restore();
				safeMutex.ReleaseLock();

				if(second_err == 0 || second_err < 0) {
					printf("** Disconnecting sock = %d\n",sock);
					if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"** Disconnecting sock = %d\n",sock);

					disconnectSocket();
				}
			}
			else {
				printf("** Disconnecting sock = %d\n",sock);
				if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"** Disconnecting sock = %d\n",sock);

				disconnectSocket();
			}

			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTED SOCKET error while peeking socket data for socket [%d], err = %d, dataSize = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,sock,err,dataSize,getLastSocketErrorFormattedText(&iErr).c_str());
		}
	}

	return static_cast<int>(err);
}

bool Socket::getBlock() {
	bool blocking = true;

	// don't waste time if the socket is invalid
	if(isSocketValid(&sock) == false) {
		return blocking;
	}

//#ifndef WIN32
//	int currentFlags = fcntl(sock, F_GETFL);
//	blocking = !((currentFlags & O_NONBLOCK) == O_NONBLOCK);
//#else
	blocking = this->isSocketBlocking;
//#endif
	return blocking;
}

void Socket::setBlock(bool block){
	setBlock(block,this->sock);
	this->isSocketBlocking = block;
}

void Socket::setBlock(bool block, PLATFORM_SOCKET socket) {
	// don't waste time if the socket is invalid
	if(isSocketValid(&socket) == false) {
		return;
	}

#ifndef WIN32
	int currentFlags = fcntl(socket, F_GETFL);
	if(currentFlags < 0) {
		currentFlags = 0;
	}
	if(block == true) {
		currentFlags &= (~O_NONBLOCK);
	}
	else {
		currentFlags |= O_NONBLOCK;
	}
	int err= fcntl(socket, F_SETFL, currentFlags);
#else
	u_long iMode= !block;
	int err= ioctlsocket(socket, FIONBIO, &iMode);
#endif
	if(err < 0) {
		throwException("Error setting I/O mode for socket");
	}
}

inline bool Socket::isReadable(bool lockMutex) {
    if(isSocketValid() == false) return false;

    struct timeval tv;
	tv.tv_sec= 0;
	tv.tv_usec= 0;

	fd_set set;
	FD_ZERO(&set);

	Mutex *lockMutexObj = (lockMutex == true ? dataSynchAccessorRead : NULL);
	MutexSafeWrapper safeMutex(lockMutexObj,CODE_AT_LINE);
	//if(lockMutex == true) {
	//	safeMutex.setMutex(dataSynchAccessorRead,CODE_AT_LINE);
	//}
	FD_SET(sock, &set);
	int i = select((int)sock + 1, &set, NULL, NULL, &tv);
	safeMutex.ReleaseLock();

	if(i < 0) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s] error while selecting socket data, err = %d, error = %s\n",__FILE__,__FUNCTION__,i,getLastSocketErrorFormattedText().c_str());
		printf("In [%s::%s] Line: %d, ERROR SELECTING SOCKET DATA retval = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,i,getLastSocketErrorFormattedText().c_str());
	}

	bool result = (i == 1);
	//if(result == false) {
	//	SystemFlags::OutputDebug(SystemFlags::debugError,"SOCKET DISCONNECTED In [%s::%s Line: %d] i = %d sock = %d\n",__FILE__,__FUNCTION__,__LINE__,i,sock);
	//}
	return result;
}

inline bool Socket::isWritable(struct timeval *timeVal, bool lockMutex) {
    if(isSocketValid() == false) return false;

	struct timeval tv;
	if(timeVal != NULL) {
		tv = *timeVal;
	}
	else {
		tv.tv_sec= 0;
		tv.tv_usec= 0;
	}

	fd_set set;
	FD_ZERO(&set);

	Mutex *lockMutexObj = (lockMutex == true ? dataSynchAccessorWrite : NULL);
	MutexSafeWrapper safeMutex(lockMutexObj,CODE_AT_LINE);
//	MutexSafeWrapper safeMutex(NULL,CODE_AT_LINE);
//	if(lockMutex == true) {
//		safeMutex.setMutex(dataSynchAccessorWrite,CODE_AT_LINE);
//	}
	FD_SET(sock, &set);
	int i = select((int)sock + 1, NULL, &set, NULL, &tv);
	safeMutex.ReleaseLock();

	bool result = false;
	if(i < 0 ) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] error while selecting socket data, err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,i,getLastSocketErrorFormattedText().c_str());

		SystemFlags::OutputDebug(SystemFlags::debugError,"SOCKET DISCONNECTED In [%s::%s Line: %d] error while selecting socket data, err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,i,getLastSocketErrorFormattedText().c_str());
	}
	else if(i == 0) {
		//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] TIMEOUT while selecting socket data, err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,i,getLastSocketErrorFormattedText().c_str());
		// Assume we are still connected, write buffer could be very busy
		result = true;
		if(difftime(time(NULL),lastSocketError) > 1) {
			lastSocketError = time(NULL);
			SystemFlags::OutputDebug(SystemFlags::debugError,"SOCKET WRITE TIMEOUT In [%s::%s Line: %d] i = %d sock = %d [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,sock,ipAddress.c_str());
		}
	}
	else {
		result = true;
	}

	//return (i == 1 && FD_ISSET(sock, &set));
	return result;
}

bool Socket::isConnected() {
	if(isSocketValid() == false) return false;

//	MutexSafeWrapper safeMutexSocketDestructorFlag(&inSocketDestructorSynchAccessor,CODE_AT_LINE);
//	if(this->inSocketDestructor == true) {
//		SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] this->inSocketDestructor == true\n",__FILE__,__FUNCTION__,__LINE__);
//		return false;
//	}
//	inSocketDestructorSynchAccessor->setOwnerId(CODE_AT_LINE);

	//if the socket is not writable then it is not conencted
	if(isWritable(NULL,true) == false) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] ERROR isWritable failed.\n",__FILE__,__FUNCTION__,__LINE__);
		return false;
	}
	//if the socket is readable it is connected if we can read a byte from it
	if(isReadable(true)) {
		char tmp=0;
		int peekDataBytes=1;
		int lastSocketError=0;

		int err = peek(&tmp, peekDataBytes, false, &lastSocketError);
		//if(err <= 0 && err != PLATFORM_SOCKET_TRY_AGAIN) {
		//if(err <= 0 && lastSocketError != 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) {
		//if((err < 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) || (err == 0 && peekDataBytes != 0) ||
		//	((err == 0 || err == -1) && peekDataBytes == 0 && lastSocketError != 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN)) {
		if((err < 0 && lastSocketError != PLATFORM_SOCKET_TRY_AGAIN) || (err == 0 && peekDataBytes != 0)) {
			//printf("IsConnected socket has disconnected sock = %d err = %d lastSocketError = %d\n",sock,err,lastSocketError);
			if(err == 0) {
				printf("IsConnected socket has disconnected sock = %d err = %d lastSocketError = %d\n",sock,err,lastSocketError);
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] ERROR Peek failed, err = %d for socket: %d, error = %s, lastSocketError = %d\n",__FILE__,__FUNCTION__,__LINE__,err,sock,getLastSocketErrorFormattedText().c_str(),lastSocketError);
			}

			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] ERROR Peek failed, err = %d for socket: %d, error = %s, lastSocketError = %d\n",__FILE__,__FUNCTION__,__LINE__,err,sock,getLastSocketErrorFormattedText().c_str(),lastSocketError);
			if(SystemFlags::VERBOSE_MODE_ENABLED) SystemFlags::OutputDebug(SystemFlags::debugError,"SOCKET DISCONNECTED In [%s::%s Line: %d] ERROR Peek failed, err = %d for socket: %d, error = %s, lastSocketError = %d\n",__FILE__,__FUNCTION__,__LINE__,err,sock,getLastSocketErrorFormattedText().c_str(),lastSocketError);
			return false;
		}
		if (isSocketValid() == false) return false;
	}

	//otherwise the socket is connected
	return true;
}

string Socket::getHostName()  {
	if(Socket::host_name == "") {
		const int strSize= 257;
		char hostname[strSize]="";
		int result = gethostname(hostname, strSize);
		if(result == 0) {
			Socket::host_name = (hostname[0] != '\0' ? hostname : "");
		}
		else {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] result = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,result,getLastSocketErrorText());
		}
	}
	return Socket::host_name;
}

string Socket::getIp() {
	hostent* info= gethostbyname(getHostName().c_str());
	unsigned char* address;

	if(info==NULL){
		throw megaglest_runtime_error("Error getting host by name");
	}

	address= reinterpret_cast<unsigned char*>(info->h_addr_list[0]);

	if(address==NULL){
		throw megaglest_runtime_error("Error getting host ip");
	}

	return
		intToStr(address[0]) + "." +
		intToStr(address[1]) + "." +
		intToStr(address[2]) + "." +
		intToStr(address[3]);
}

void Socket::throwException(string str){
	string msg = str + " " + getLastSocketErrorFormattedText();
	throw megaglest_runtime_error(msg);
}

// ===============================================
//	class ClientSocket
// ===============================================

ClientSocket::ClientSocket() : Socket() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	stopBroadCastClientThread();

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

ClientSocket::~ClientSocket() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	stopBroadCastClientThread();

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

void ClientSocket::stopBroadCastClientThread() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	if(broadCastClientThread != NULL) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
		broadCastClientThread->shutdownAndWait();
		delete broadCastClientThread;
		broadCastClientThread = NULL;
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
	}

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

void ClientSocket::startBroadCastClientThread(DiscoveredServersInterface *cb) {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	ClientSocket::stopBroadCastClientThread();

	static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__);
	broadCastClientThread = new BroadCastClientSocketThread(cb);
	broadCastClientThread->setUniqueID(mutexOwnerId);
	broadCastClientThread->start();

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

void ClientSocket::discoverServers(DiscoveredServersInterface *cb) {
	ClientSocket::startBroadCastClientThread(cb);
}

void ClientSocket::connect(const Ip &ip, int port)
{
	sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));

    addr.sin_family= AF_INET;
	addr.sin_addr.s_addr= inet_addr(ip.getString().c_str());
	addr.sin_port= htons(port);

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Connecting to host [%s] on port = %d\n", ip.getString().c_str(),port);
	if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("Connecting to host [%s] on port = %d\n", ip.getString().c_str(),port);

    connectedIpAddress = "";
	int err= ::connect(sock, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr));
	if(err < 0)	{
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #2 Error connecting socket for IP: %s for Port: %d err = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,ip.getString().c_str(),port,err,getLastSocketErrorFormattedText().c_str());
	    if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] #2 Error connecting socket for IP: %s for Port: %d err = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,ip.getString().c_str(),port,err,getLastSocketErrorFormattedText().c_str());

		int lastSocketError = getLastSocketError();
        if (lastSocketError == PLATFORM_SOCKET_INPROGRESS ||
        	lastSocketError == PLATFORM_SOCKET_TRY_AGAIN) {
            fd_set myset;
            struct timeval tv;
            int valopt=0;
            socklen_t lon=0;

            if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] PLATFORM_SOCKET_INPROGRESS in connect() - selecting\n",__FILE__,__FUNCTION__,__LINE__);
            if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] PLATFORM_SOCKET_INPROGRESS in connect() - selecting\n",__FILE__,__FUNCTION__,__LINE__);

            do {
               tv.tv_sec = 10;
               tv.tv_usec = 0;

               FD_ZERO(&myset);
               FD_SET(sock, &myset);

               {
            	   MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
            	   err = select((int)sock + 1, NULL, &myset, NULL, &tv);
				   lastSocketError = getLastSocketError();
            	   //safeMutex.ReleaseLock();
               }

               if (err < 0 && lastSocketError != PLATFORM_SOCKET_INTERRUPTED) {
            	   if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Error connecting %s\n",__FILE__,__FUNCTION__,__LINE__,getLastSocketErrorFormattedText().c_str());
            	   if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Error connecting %s\n",__FILE__,__FUNCTION__,__LINE__,getLastSocketErrorFormattedText().c_str());

                  break;
               }
               else if(err > 0) {
               //else if(FD_ISSET(sock, &myset)) {
                  // Socket selected for write
                  lon = sizeof(valopt);
#ifndef WIN32
                  if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0)
#else
                  if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)(&valopt), &lon) < 0)
#endif
                  {
                	  if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Error in getsockopt() %s\n",__FILE__,__FUNCTION__,__LINE__,getLastSocketErrorFormattedText().c_str());
                	  if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Error in getsockopt() %s\n",__FILE__,__FUNCTION__,__LINE__,getLastSocketErrorFormattedText().c_str());
                     break;
                  }
                  // Check the value returned...
                  if(valopt) {
                	  if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Error in delayed connection() %d - [%s]\n",__FILE__,__FUNCTION__,__LINE__,valopt, strerror(valopt));
                	  if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Error in delayed connection() %d - [%s]\n",__FILE__,__FUNCTION__,__LINE__,valopt, strerror(valopt));
                     break;
                  }

                  errno = 0;
                  if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Apparent recovery for connection sock = %d, err = %d\n",__FILE__,__FUNCTION__,__LINE__,sock,err);
                  if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Apparent recovery for connection sock = " PLATFORM_SOCKET_FORMAT_TYPE ", err = %d\n",__FILE__,__FUNCTION__,__LINE__,sock,err);

                  break;
               }
               else {
            	   if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Timeout in select() - Cancelling!\n",__FILE__,__FUNCTION__,__LINE__);
            	   if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Timeout in select() - Cancelling!\n",__FILE__,__FUNCTION__,__LINE__);

                  disconnectSocket();
                  break;
               }
            } while (1);
        }

        if(err < 0) {
        	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Before END sock = %d, err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,sock,err,getLastSocketErrorFormattedText().c_str());
        	if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Before END sock = " PLATFORM_SOCKET_FORMAT_TYPE ", err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,sock,err,getLastSocketErrorFormattedText().c_str());
            disconnectSocket();
        }
        else {
        	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Valid recovery for connection sock = %d, err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,sock,err,getLastSocketErrorFormattedText().c_str());
        	if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Valid recovery for connection sock = " PLATFORM_SOCKET_FORMAT_TYPE ", err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,sock,err,getLastSocketErrorFormattedText().c_str());
        	connectedIpAddress = ip.getString();
        }
	}
	else {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Connected to host [%s] on port = %d sock = %d err = %d", ip.getString().c_str(),port,sock,err);
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Connected to host [%s] on port = %d sock = " PLATFORM_SOCKET_FORMAT_TYPE " err = %d", ip.getString().c_str(),port,sock,err);
		connectedIpAddress = ip.getString();
	}
}

//=======================================================================
// Function :		discovery response thread
// Description:		Runs in its own thread to listen for broadcasts from
//					other servers
//
BroadCastClientSocketThread::BroadCastClientSocketThread(DiscoveredServersInterface *cb) : BaseThread() {

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	discoveredServersCB = cb;
	uniqueID = "BroadCastClientSocketThread";

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

//=======================================================================
// Function :		broadcast thread
// Description:		Runs in its own thread to send out a broadcast to the local network
//					the current broadcast message is <myhostname:my.ip.address.dotted>
//
void BroadCastClientSocketThread::execute() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
	RunningStatusSafeWrapper runningStatus(this);

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcast Client thread is running\n");

	std::vector<string> foundServers;

	short port;                 // The port for the broadcast.
    struct sockaddr_in bcSender; // local socket address for the broadcast.
    struct sockaddr_in bcaddr;  // The broadcast address for the receiver.
    PLATFORM_SOCKET bcfd;                // The file descriptor used for the broadcast.
    //bool one = true;            // Parameter for "setscokopt".
    socklen_t alen=0;

    port = htons( Socket::getBroadCastPort() );

    // Prepare to receive the broadcast.
    bcfd = socket(AF_INET, SOCK_DGRAM, 0);
    if( bcfd <= 0 )	{
    	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"socket failed: %s\n", Socket::getLastSocketErrorFormattedText().c_str());
	}
    else {
		// Create the address we are receiving on.
		memset( (char*)&bcaddr, 0, sizeof(bcaddr));
		bcaddr.sin_family = AF_INET;
		bcaddr.sin_addr.s_addr = htonl(INADDR_ANY);
		bcaddr.sin_port = port;

		int val = 1;
#ifndef WIN32
		int opt_result = setsockopt(bcfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
#else
		int opt_result = setsockopt(bcfd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
#endif
		if(::bind( bcfd,  (struct sockaddr *)&bcaddr, sizeof(bcaddr) ) < 0 )	{
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"bind failed: %s opt_result = %d\n", Socket::getLastSocketErrorFormattedText().c_str(),opt_result);
		}
		else {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] opt_result = %d\n",__FILE__,__FUNCTION__,__LINE__,opt_result);

			Socket::setBlock(false, bcfd);

			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

			try	{
				char buff[10024]="";  // Buffers the data to be broadcasted.
				// Keep getting packets forever.
				for( time_t elapsed = time(NULL); difftime((long int)time(NULL),elapsed) <= 5; ) {
					alen = sizeof(struct sockaddr);
					int nb=0;// The number of bytes read.
					bool gotData = (nb = recvfrom(bcfd, buff, 10024, 0, (struct sockaddr *) &bcSender, &alen)) > 0;

					//printf("Broadcasting client nb = %d buff [%s] gotData = %d\n",nb,buff,gotData);

					if(gotData == false) {
						if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"recvfrom failed: %s\n", Socket::getLastSocketErrorFormattedText().c_str());
					}
					else {
						//string fromIP = inet_ntoa(bcSender.sin_addr);
						char szHostFrom[100]="";
						Ip::Inet_NtoA(SockAddrToUint32(&bcSender.sin_addr), szHostFrom);
						//printf("Client szHostFrom [%s]\n",szHostFrom);

						string fromIP = szHostFrom;
						if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"broadcast message received: [%s] from: [%s]\n", buff,fromIP.c_str() );

						if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Client got broadcast from server: [%s] data [%s]\n",fromIP.c_str(),buff);

						int serverGamePort = Socket::getBroadCastPort();
						vector<string> paramPartPortsTokens;
						Tokenize(buff,paramPartPortsTokens,":");
						if(paramPartPortsTokens.size() >= 3 && paramPartPortsTokens[2].length() > 0) {
							serverGamePort = strToInt(paramPartPortsTokens[2]);
						}

						if(std::find(foundServers.begin(),foundServers.end(),fromIP) == foundServers.end()) {
							foundServers.push_back(fromIP + ":" + intToStr(serverGamePort));
						}

						// For now break as soon as we find a server
						break;
					}

					if(getQuitStatus() == true) {
						if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
						break;
					}
					sleep( 100 ); // send out broadcast every 1 seconds
					if(getQuitStatus() == true) {
						if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
						break;
					}
				}
			}
			catch(const exception &ex) {
				SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
			}
			catch(...) {
				SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",__FILE__,__FUNCTION__,__LINE__);
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] unknown error\n",__FILE__,__FUNCTION__,__LINE__);
			}
		}
    }

#ifndef WIN32
    if(bcfd >= 0) ::close(bcfd);
    bcfd = -1;
#else
    if(bcfd != INVALID_SOCKET) ::closesocket(bcfd);
    bcfd = INVALID_SOCKET;
#endif

    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

    // Here we callback into the implementer class
    if(getQuitStatus() == false && discoveredServersCB != NULL) {
    	discoveredServersCB->DiscoveredServers(foundServers);
    }

    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcast Client thread is exiting\n");
    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

// ===============================================
//	class ServerSocket
// ===============================================

ServerSocket::ServerSocket(bool basicMode) : Socket() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] basicMode = %d\n",__FILE__,__FUNCTION__,__LINE__,basicMode);

	this->basicMode = basicMode;
	this->bindSpecificAddress = "";
	//printf("SERVER SOCKET CONSTRUCTOR\n");

	boundPort = 0;
	portBound = false;
	broadCastThread = NULL;
	if(this->basicMode == false) {
		UPNP_Tools::enabledUPNP = false;
	}

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

ServerSocket::~ServerSocket() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	//printf("In [%s::%s] Line: %d UPNP_Tools::enabledUPNP = %d\n",__FILE__,__FUNCTION__,__LINE__,UPNP_Tools::enabledUPNP);

	stopBroadCastThread();

	if(this->basicMode == false) {
		MutexSafeWrapper safeMutexUPNP(&ServerSocket::mutexUpnpdiscoverThread,CODE_AT_LINE);
		if(ServerSocket::upnpdiscoverThread != NULL) {

			ServerSocket::cancelUpnpdiscoverThread = true;
			SDL_WaitThread(ServerSocket::upnpdiscoverThread, NULL);
			ServerSocket::upnpdiscoverThread = NULL;
			ServerSocket::cancelUpnpdiscoverThread = false;
		}
		safeMutexUPNP.ReleaseLock();
		if (UPNP_Tools::enabledUPNP) {
			UPNP_Tools::NETremRedirects(this->getExternalPort());
		}

		MutexSafeWrapper safeMutexUPNP1(&UPNP_Tools::mutexUPNP,CODE_AT_LINE);
		if(urls.controlURL && urls.ipcondescURL && urls.controlURL_CIF) {
			FreeUPNPUrls(&urls);
		}

		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
		safeMutexUPNP1.ReleaseLock();
	}
}

void ServerSocket::stopBroadCastThread() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	//printf("Stopping broadcast thread [%p]\n",broadCastThread);

	if(broadCastThread != NULL) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

		//printf("Stopping broadcast thread [%p] - A\n",broadCastThread);

		if(broadCastThread->canShutdown(false) == true) {
			sleep(0);
		}
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
		//printf("Stopping broadcast thread [%p] - B\n",broadCastThread);

		if(broadCastThread->shutdownAndWait() == true) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
			delete broadCastThread;

			//printf("Stopping broadcast thread [%p] - C\n",broadCastThread);
		}
		broadCastThread = NULL;
		//printf("Stopping broadcast thread [%p] - D\n",broadCastThread);

		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
	}

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

void ServerSocket::startBroadCastThread() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	stopBroadCastThread();

	broadCastThread = new BroadCastSocketThread(this->boundPort);

	//printf("Start broadcast thread [%p]\n",broadCastThread);

	static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__);
	broadCastThread->setUniqueID(mutexOwnerId);
	broadCastThread->start();

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

void ServerSocket::resumeBroadcast() {
	if(broadCastThread != NULL) {
		broadCastThread->setPauseBroadcast(false);
	}
}
void ServerSocket::pauseBroadcast() {
	if(broadCastThread != NULL) {
		broadCastThread->setPauseBroadcast(true);
	}
}

bool ServerSocket::isBroadCastThreadRunning() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	bool isThreadRunning = (broadCastThread != NULL && broadCastThread->getRunningStatus() == true);

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] isThreadRunning = %d\n",__FILE__,__FUNCTION__,__LINE__,isThreadRunning);

	return isThreadRunning;
}

void ServerSocket::bind(int port) {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d port = %d, portBound = %d START\n",__FILE__,__FUNCTION__,__LINE__,port,portBound);

	boundPort = port;

	if(isSocketValid() == false) {
		sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if(isSocketValid() == false) {
			throwException("Error creating socket");
		}
		setBlock(false);
	}

	//sockaddr structure
	sockaddr_in addr;
	addr.sin_family= AF_INET;
	if(this->bindSpecificAddress != "") {
		addr.sin_addr.s_addr= inet_addr(this->bindSpecificAddress.c_str());
	}
	else {
		addr.sin_addr.s_addr= INADDR_ANY;
	}
	addr.sin_port= htons(port);
	addr.sin_zero[0] = 0;

	int val = 1;

#ifndef WIN32
	int opt_result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
#else
	int opt_result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
#endif

	int err= ::bind(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
	if(err < 0) {
	    char szBuf[8096]="";
	    snprintf(szBuf, 8096,"In [%s::%s] Error binding socket sock = " PLATFORM_SOCKET_FORMAT_TYPE ", address [%s] port = %d err = %d, error = %s opt_result = %d\n",__FILE__,__FUNCTION__,sock,this->bindSpecificAddress.c_str(),port,err,getLastSocketErrorFormattedText().c_str(),opt_result);
	    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"%s",szBuf);

	    snprintf(szBuf, 8096,"Error binding socket sock = " PLATFORM_SOCKET_FORMAT_TYPE ", address [%s] port = %d err = %d, error = %s\n",sock,this->bindSpecificAddress.c_str(),port,err,getLastSocketErrorFormattedText().c_str());
	    throw megaglest_runtime_error(szBuf);
	}
	portBound = true;

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d port = %d, portBound = %d END\n",__FILE__,__FUNCTION__,__LINE__,port,portBound);
}

void ServerSocket::disconnectSocket() {
	Socket::disconnectSocket();
	portBound = false;
}

void ServerSocket::listen(int connectionQueueSize) {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d connectionQueueSize = %d\n",__FILE__,__FUNCTION__,__LINE__,connectionQueueSize);

	if(connectionQueueSize > 0) {
		if(isSocketValid() == false) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

			sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
			if(isSocketValid() == false) {
				throwException("Error creating socket");
			}
			setBlock(false);
		}

		if(portBound == false) {
			bind(boundPort);
		}

		int err= ::listen(sock, connectionQueueSize);
		if(err < 0) {
			char szBuf[8096]="";
			snprintf(szBuf, 8096,"In [%s::%s] Error listening socket sock = " PLATFORM_SOCKET_FORMAT_TYPE ", err = %d, error = %s\n",__FILE__,__FUNCTION__,sock,err,getLastSocketErrorFormattedText().c_str());
			throwException(szBuf);
		}
	}
	else {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
		disconnectSocket();
	}

	if(this->basicMode == false) {
		if(connectionQueueSize > 0) {
			if(isBroadCastThreadRunning() == false) {
				startBroadCastThread();
			}
			else {
				resumeBroadcast();
			}
		}
		else {
			pauseBroadcast();
		}
	}
}

Socket *ServerSocket::accept(bool errorOnFail) {
	if(isSocketValid() == false) {
		if(errorOnFail == true) {
			throwException("socket is invalid!");
		}
		else {
			return NULL;
		}
	}

	PLATFORM_SOCKET newSock=0;
	char client_host[100]="";
	//const int max_attempts = 100;
	//for(int attempt = 0; attempt < max_attempts; ++attempt) {
		struct sockaddr_in cli_addr;
		socklen_t clilen = sizeof(cli_addr);
		client_host[0]='\0';
		MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
		newSock= ::accept(sock, (struct sockaddr *) &cli_addr, &clilen);
		safeMutex.ReleaseLock();

		if(isSocketValid(&newSock) == false) {
			char szBuf[8096]="";
			snprintf(szBuf, 8096,"In [%s::%s Line: %d] Error accepting socket connection sock = " PLATFORM_SOCKET_FORMAT_TYPE ", err = " PLATFORM_SOCKET_FORMAT_TYPE ", error = %s\n",__FILE__,__FUNCTION__,__LINE__,sock,newSock,getLastSocketErrorFormattedText().c_str());
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,szBuf);

			int lastSocketError = getLastSocketError();
			if(lastSocketError == PLATFORM_SOCKET_TRY_AGAIN) {
				//if(attempt+1 >= max_attempts) {
				//	return NULL;
				//}
				//else {
					sleep(0);
				//}
			}
			if(errorOnFail == true) {
				throwException(szBuf);
			}
			else {
	#ifndef WIN32
			::close(newSock);
			newSock = -1;
	#else
			::closesocket(newSock);
			newSock = INVALID_SOCKET;
	#endif

				return NULL;
			}

		}
		else {
			Ip::Inet_NtoA(SockAddrToUint32((struct sockaddr *)&cli_addr), client_host);
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got connection, newSock = %d client_host [%s]\n",__FILE__,__FUNCTION__,__LINE__,newSock,client_host);
		}
		if(isIPAddressBlocked((client_host[0] != '\0' ? client_host : "")) == true) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] BLOCKING connection, newSock = %d client_host [%s]\n",__FILE__,__FUNCTION__,__LINE__,newSock,client_host);

	#ifndef WIN32
			::close(newSock);
			newSock = -1;
	#else
			::closesocket(newSock);
			newSock = INVALID_SOCKET;
	#endif

			return NULL;
		}

		//break;
	//}
	Socket *result = new Socket(newSock);
	result->setIpAddress((client_host[0] != '\0' ? client_host : ""));
	return result;
}

void ServerSocket::NETdiscoverUPnPDevices() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] UPNP - Start\n",__FILE__,__FUNCTION__,__LINE__);

	MutexSafeWrapper safeMutexUPNP(&ServerSocket::mutexUpnpdiscoverThread,CODE_AT_LINE);
	if(ServerSocket::upnpdiscoverThread != NULL) {
		ServerSocket::cancelUpnpdiscoverThread = true;
		SDL_WaitThread(ServerSocket::upnpdiscoverThread, NULL);
		ServerSocket::upnpdiscoverThread = NULL;
		ServerSocket::cancelUpnpdiscoverThread = false;
	}

    // WATCH OUT! Because the thread takes void * as a parameter we MUST cast to the pointer type
    // used on the other side (inside the thread)
	//printf("STARTING UPNP Thread\n");
	ServerSocket::upnpdiscoverThread = SDL_CreateThread(&UPNP_Tools::upnp_init, "upnpdiscoverThread", dynamic_cast<UPNPInitInterface *>(this));
	safeMutexUPNP.ReleaseLock();
	//printf("In [%s::%s] Line: %d safeMutexUPNP\n",__FILE__,__FUNCTION__,__LINE__);

	//printf("SERVER SOCKET NETdiscoverUPnPDevices - END\n");
}

void ServerSocket::UPNPInitStatus(bool result) {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] result = %d\n",__FILE__,__FUNCTION__,__LINE__,result);

    if(result == true) {
        //int ports[4] = { this->getExternalPort(), this->getBindPort(), this->getFTPServerPort(), this->getFTPServerPort() };
        std::vector<int> UPNPPortForwardList;

        // Glest Game Server port
        UPNPPortForwardList.push_back(this->getExternalPort());
        UPNPPortForwardList.push_back(this->getBindPort());

        // Glest mini FTP Server Listen Port
        UPNPPortForwardList.push_back(this->getFTPServerPort());
        UPNPPortForwardList.push_back(this->getFTPServerPort());

        // GLest mini FTP Server Passive file TRansfer ports (1 per game player)
        for(int clientIndex = 1; clientIndex <= ServerSocket::maxPlayerCount; ++clientIndex) {
            UPNPPortForwardList.push_back(this->getFTPServerPort() + clientIndex);
            UPNPPortForwardList.push_back(this->getFTPServerPort() + clientIndex);
        }

        UPNP_Tools::NETaddRedirects(UPNPPortForwardList,false);
    }
}

//
// UPNP Tools Start
//
void UPNP_Tools::AddUPNPPortForward(int internalPort, int externalPort) {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] internalPort = %d, externalPort = %d\n",__FILE__,__FUNCTION__,__LINE__,internalPort,externalPort);

    bool addPorts = (UPNP_Tools::enabledUPNP == true);
    if(addPorts == false) {
        int result = UPNP_Tools::upnp_init(NULL);
        addPorts = (result != 0);
    }

    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] internalPort = %d, externalPort = %d, addPorts = %d\n",__FILE__,__FUNCTION__,__LINE__,internalPort,externalPort,addPorts);

    if(addPorts == true) {
        int ports[2] = { externalPort, internalPort };
        UPNP_Tools::upnp_add_redirect(ports);
    }
}

void UPNP_Tools::RemoveUPNPPortForward(int internalPort, int externalPort) {
    UPNP_Tools::upnp_rem_redirect(externalPort);
}

//
// This code below handles Universal Plug and Play Router Discovery
//
int UPNP_Tools::upnp_init(void *param) {
	int result				= -1;
	struct UPNPDev *devlist = NULL;
	struct UPNPDev *dev     = NULL;
	int descXMLsize         = 0;
	char buf[255]           = "";
	// Callers MUST pass in NULL or a UPNPInitInterface *
	UPNPInitInterface *callback = (UPNPInitInterface *)(param);

	MutexSafeWrapper safeMutexUPNP(&UPNP_Tools::mutexUPNP,CODE_AT_LINE);
	memset(&urls, 0, sizeof(struct UPNPUrls));
	memset(&data, 0, sizeof(struct IGDdatas));

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] isUPNP = %d callback = %p\n",__FILE__,__FUNCTION__,__LINE__,UPNP_Tools::isUPNP,callback);

	if(UPNP_Tools::isUPNP == true) {
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Searching for UPnP devices for automatic port forwarding...\n");

		int upnp_delay = 5000;
		const char *upnp_multicastif = NULL;
		const char *upnp_minissdpdsock = NULL;
		int upnp_sameport = 0;
		int upnp_ipv6 = 0;
		int upnp_error = 0;

#ifndef MINIUPNPC_VERSION_PRE1_6
	#if !defined(MINIUPNPC_API_VERSION) || MINIUPNPC_API_VERSION < 14
		devlist = upnpDiscover(upnp_delay, upnp_multicastif, upnp_minissdpdsock, upnp_sameport, upnp_ipv6, &upnp_error);
	#else
		// miniupnpc 1.9.20150730
		devlist = upnpDiscover(upnp_delay, upnp_multicastif, upnp_minissdpdsock, upnp_sameport, upnp_ipv6, 2, &upnp_error);
	#endif
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"UPnP discover returned upnp_error = %d.\n",upnp_error);
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("UPnP discover returned upnp_error = %d.\n",upnp_error);

#else
		devlist = upnpDiscover(upnp_delay, upnp_multicastif, upnp_minissdpdsock, upnp_sameport);
#endif
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"UPnP device search finished devlist = %p.\n",devlist);
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("UPnP device search finished devlist = %p.\n",devlist);

		if(ServerSocket::cancelUpnpdiscoverThread == true) {
			if(devlist != NULL) {
				freeUPNPDevlist(devlist);
			}
			devlist = NULL;
			return result;
		}

		if (devlist) {
			dev = devlist;
			while (dev) {
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"UPnP discover deviceList [%s].\n",(dev->st ? dev->st : "null"));
				if(SystemFlags::VERBOSE_MODE_ENABLED) printf("UPnP discover deviceList [%s].\n",(dev->st ? dev->st : "null"));

				dev = dev->pNext;
			}

			dev = devlist;
			while (dev && dev->st) {
				if (strstr(dev->st, "InternetGatewayDevice")) {
					break;
				}
				dev = dev->pNext;
			}
			if (!dev) {
				dev = devlist; /* defaulting to first device */
			}

			if(dev != NULL) {
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"UPnP device found: %s %s\n", dev->descURL, dev->st);
				if(SystemFlags::VERBOSE_MODE_ENABLED) printf("UPnP device found: %s %s\n", dev->descURL, dev->st);

				//printf("UPnP device found: [%s] [%s] lanaddr [%s]\n", dev->descURL, dev->st,lanaddr);
#if (defined(MINIUPNPC_API_VERSION)  && MINIUPNPC_API_VERSION >= 9) || (!defined(MINIUPNPC_VERSION_PRE1_7) && !defined(MINIUPNPC_VERSION_PRE1_6))
				char *descXML = (char *)miniwget_getaddr(dev->descURL, &descXMLsize, lanaddr, (sizeof(lanaddr) / sizeof(lanaddr[0])),0);
#else
				char *descXML = (char *)miniwget_getaddr(dev->descURL, &descXMLsize, lanaddr, (sizeof(lanaddr) / sizeof(lanaddr[0])));
#endif
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"LAN address: %s\n", lanaddr);

				if (descXML) {
					parserootdesc (descXML, descXMLsize, &data);
					free (descXML); descXML = 0;

#if (defined(MINIUPNPC_API_VERSION)  && MINIUPNPC_API_VERSION >= 9) || (!defined(MINIUPNPC_VERSION_PRE1_7) && !defined(MINIUPNPC_VERSION_PRE1_6))
					GetUPNPUrls (&urls, &data, dev->descURL,0);
#else
					GetUPNPUrls (&urls, &data, dev->descURL);
#endif
				}
				snprintf(buf, 255,"UPnP device found: %s %s LAN address %s", dev->descURL, dev->st, lanaddr);

				freeUPNPDevlist(devlist);
				devlist = NULL;
			}

			if(ServerSocket::cancelUpnpdiscoverThread == true) {
				//if(devlist != NULL) {
				//	freeUPNPDevlist(devlist);
				//}
				//devlist = NULL;
				return result;
			}

			if (!urls.controlURL || urls.controlURL[0] == '\0')	{
				snprintf(buf, 255,"controlURL not available, UPnP disabled");
				if(callback) {
					safeMutexUPNP.ReleaseLock();
				    callback->UPNPInitStatus(false);
				}
				result = 0;
			}
			else {
				char externalIP[16]     = "";
#ifndef MINIUPNPC_VERSION_PRE1_5
				UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIP);
#else
				UPNP_GetExternalIPAddress(urls.controlURL, data.servicetype, externalIP);
#endif
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"UPnP device found at: [%s] callback [%p]\n",externalIP,callback);

				//UPNP_Tools::NETaddRedirects(ports);
				UPNP_Tools::enabledUPNP = true;
				if(callback) {
					safeMutexUPNP.ReleaseLock();
					if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
					callback->UPNPInitStatus(true);
					if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
				}
				result = 1;
			}
		}

		if(result == -1) {
			snprintf(buf, 255,"UPnP device not found.");

			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"No UPnP devices found.\n");

			if(ServerSocket::cancelUpnpdiscoverThread == true) {
				//if(devlist != NULL) {
				//	freeUPNPDevlist(devlist);
				//}
				//devlist = NULL;
				return result;
			}

			if(callback) {
				safeMutexUPNP.ReleaseLock();
				callback->UPNPInitStatus(false);
			}
			result = 0;
		}
	}
	else {
		snprintf(buf, 255,"UPnP detection routine disabled by user.");
		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"UPnP detection routine disabled by user.\n");

		if(ServerSocket::cancelUpnpdiscoverThread == true) {
			//if(devlist != NULL) {
			//	freeUPNPDevlist(devlist);
			//}
			//devlist = NULL;
			return result;
		}

        if(callback) {
        	safeMutexUPNP.ReleaseLock();
            callback->UPNPInitStatus(false);
        }
        result = 0;
	}

	//printf("ENDING UPNP Thread\n");

	return result;
}

bool UPNP_Tools::upnp_add_redirect(int ports[2],bool mutexLock) {
	bool result				= true;

	//printf("SERVER SOCKET upnp_add_redirect - START\n");
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] upnp_add_redir(%d : %d)\n",__FILE__,__FUNCTION__,__LINE__,ports[0],ports[1]);

	if(mutexLock) {
		MutexSafeWrapper safeMutexUPNP(&ServerSocket::mutexUpnpdiscoverThread,CODE_AT_LINE);
		if(ServerSocket::upnpdiscoverThread != NULL) {
			ServerSocket::cancelUpnpdiscoverThread = true;
			SDL_WaitThread(ServerSocket::upnpdiscoverThread, NULL);
			ServerSocket::upnpdiscoverThread = NULL;
			ServerSocket::cancelUpnpdiscoverThread = false;
		}
		safeMutexUPNP.ReleaseLock();
	}

	MutexSafeWrapper safeMutexUPNP(&UPNP_Tools::mutexUPNP,CODE_AT_LINE);
	if (!urls.controlURL || urls.controlURL[0] == '\0')	{
		result = false;
	}
	else {
		char externalIP[16] = "";
#ifndef MINIUPNPC_VERSION_PRE1_5
		UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIP);
#else
		UPNP_GetExternalIPAddress(urls.controlURL, data.servicetype, externalIP);
#endif

		char ext_port_str[16]   = "";
		char int_port_str[16]   = "";
		sprintf(ext_port_str, "%d", ports[0]);
		sprintf(int_port_str, "%d", ports[1]);

		//int r                   = 0;
#ifndef MINIUPNPC_VERSION_PRE1_5
	#ifndef MINIUPNPC_VERSION_PRE1_6
		int r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,ext_port_str, int_port_str, lanaddr, "MegaGlest - www.megaglest.org", "TCP", 0, NULL);
	#else
		int r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,ext_port_str, int_port_str, lanaddr, "MegaGlest - www.megaglest.org", "TCP", 0);
	#endif
#else
		int r = UPNP_AddPortMapping(urls.controlURL, data.servicetype,ext_port_str, int_port_str, lanaddr, "MegaGlest - www.megaglest.org", "TCP", 0);
#endif
		if (r != UPNPCOMMAND_SUCCESS) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] AddPortMapping(%s, %s, %s) failed\n",__FILE__,__FUNCTION__,__LINE__,ext_port_str, int_port_str, lanaddr);
			result = false;
		}

		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] AddPortMapping(%s, %s, %s) success\n",__FILE__,__FUNCTION__,__LINE__,ext_port_str, int_port_str, lanaddr);
	}

	//printf("SERVER SOCKET upnp_add_redirect - END [%d]\n",result);
	return result;
}

void UPNP_Tools::upnp_rem_redirect(int ext_port) {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] upnp_rem_redir(%d)\n",__FILE__,__FUNCTION__,__LINE__,ext_port);

	MutexSafeWrapper safeMutexUPNP(&ServerSocket::mutexUpnpdiscoverThread,CODE_AT_LINE);
    if(ServerSocket::upnpdiscoverThread != NULL) {
    	ServerSocket::cancelUpnpdiscoverThread = true;
        SDL_WaitThread(ServerSocket::upnpdiscoverThread, NULL);
        ServerSocket::upnpdiscoverThread = NULL;
        ServerSocket::cancelUpnpdiscoverThread = false;
    }
    safeMutexUPNP.ReleaseLock();

    MutexSafeWrapper safeMutexUPNP1(&UPNP_Tools::mutexUPNP,CODE_AT_LINE);
	if (urls.controlURL && urls.controlURL[0] != '\0')	{
		char ext_port_str[16]="";
		sprintf(ext_port_str, "%d", ext_port);

		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n\n#1 DEBUGGING urls.controlURL [%s]\n",urls.controlURL);

		int result = 0;
	#ifndef MINIUPNPC_VERSION_PRE1_5
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n\n#1 DEBUGGING data.first.servicetype [%s]\n",data.first.servicetype);
		result = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, ext_port_str, "TCP", 0);
	#else
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n\n#1 DEBUGGING data.servicetype [%s]\n",data.servicetype);
		result = UPNP_DeletePortMapping(urls.controlURL, data.servicetype, ext_port_str, "TCP", 0);
	#endif
		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n\nresult = %d\n",result);
		//printf("\n\nresult = %d\n",result);
	}
	if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n\n#2 DEBUGGING urls.controlURL [%s]\n",urls.controlURL);

	//printf("SERVER SOCKET upnp_rem_redirect - END\n");
}

void UPNP_Tools::NETaddRedirects(std::vector<int> UPNPPortForwardList,bool mutexLock) {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] upnp_rem_redir(%d)\n",__FILE__,__FUNCTION__,__LINE__);

    if(UPNPPortForwardList.size() % 2 != 0) {
        // We need groups of 2 ports.. one external and one internal for opening ports on UPNP router
        throw megaglest_runtime_error("UPNPPortForwardList.size() MUST BE divisable by 2");
    }

    for(unsigned int clientIndex = 0; clientIndex < UPNPPortForwardList.size(); clientIndex += 2) {
        int ports[2] = { UPNPPortForwardList[clientIndex], UPNPPortForwardList[clientIndex+1] };
        upnp_add_redirect(ports,mutexLock);
    }
}

void UPNP_Tools::NETremRedirects(int ext_port) {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] upnp_rem_redir(%d)\n",__FILE__,__FUNCTION__,__LINE__);
    upnp_rem_redirect(ext_port);
}
//
// UPNP Tools END
//

//=======================================================================
// Function :		broadcast_thread
// in		:		none
// return	:		none
// Description:		To be forked in its own thread to send out a broadcast to the local subnet
//					the current broadcast message is <myhostname:my.ip.address.dotted>
//
BroadCastSocketThread::BroadCastSocketThread(int boundPort) : BaseThread() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
	mutexPauseBroadcast = new Mutex(CODE_AT_LINE);
	setPauseBroadcast(false);
	this->boundPort = boundPort;
	uniqueID = "BroadCastSocketThread";
	//printf("new broadcast thread [%p]\n",this);
}

BroadCastSocketThread::~BroadCastSocketThread() {
	//printf("delete broadcast thread [%p]\n",this);

	delete mutexPauseBroadcast;
	mutexPauseBroadcast = NULL;
}

bool BroadCastSocketThread::getPauseBroadcast() {
	MutexSafeWrapper safeMutexSocketDestructorFlag(mutexPauseBroadcast,CODE_AT_LINE);
	mutexPauseBroadcast->setOwnerId(CODE_AT_LINE);
	return pauseBroadcast;
}

void BroadCastSocketThread::setPauseBroadcast(bool value) {
	MutexSafeWrapper safeMutexSocketDestructorFlag(mutexPauseBroadcast,CODE_AT_LINE);
	mutexPauseBroadcast->setOwnerId(CODE_AT_LINE);
	pauseBroadcast = value;
}


bool BroadCastSocketThread::canShutdown(bool deleteSelfIfShutdownDelayed) {
	bool ret = (getExecutingTask() == false);
	if(ret == false && deleteSelfIfShutdownDelayed == true) {
	    setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed);
	    deleteSelfIfRequired();
	    signalQuit();
	}

	return ret;
}

void BroadCastSocketThread::execute() {
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
	//setRunningStatus(true);
	RunningStatusSafeWrapper runningStatus(this);
	ExecutingTaskSafeWrapper safeExecutingTaskMutex(this);

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcast thread is running\n");

	const int MAX_NIC_COUNT = 10;
    short port=0;                 // The port for the broadcast.
    struct sockaddr_in bcLocal[MAX_NIC_COUNT]; // local socket address for the broadcast.
    PLATFORM_SOCKET bcfd[MAX_NIC_COUNT];                // The socket used for the broadcast.
    int one = 1;            // Parameter for "setscokopt".
    int pn=0;                     // The number of the packet broadcasted.
    const int buffMaxSize=1024;
    char buff[buffMaxSize]="";            // Buffers the data to be broadcasted.
    char myhostname[100]="";       // hostname of local machine
    //char subnetmask[MAX_NIC_COUNT][100];       // Subnet mask to broadcast to
    //struct hostent* myhostent=NULL;

    for(unsigned int idx = 0; idx < (unsigned int)MAX_NIC_COUNT; idx++) {
    	memset( &bcLocal[idx], 0, sizeof( struct sockaddr_in));

#ifdef WIN32
		bcfd[idx] = INVALID_SOCKET;
#else
		bcfd[idx] = -1;
#endif
    }
    /* get my host name */
    gethostname(myhostname,100);
    //struct hostent*myhostent = gethostbyname(myhostname);

    // get all host IP addresses
    std::vector<std::string> ipList = Socket::getLocalIPAddressList();

    // Subnet, IP Address
    std::vector<std::string> ipSubnetMaskList;
	for(unsigned int idx = 0; idx < (unsigned int)ipList.size() && idx < (unsigned int)MAX_NIC_COUNT; idx++) {
		string broadCastAddress = getNetworkInterfaceBroadcastAddress(ipList[idx]);
		//printf("idx = %d broadCastAddress [%s]\n",idx,broadCastAddress.c_str());

		//strcpy(subnetmask[idx], broadCastAddress.c_str());
		if(broadCastAddress != "" && std::find(ipSubnetMaskList.begin(),ipSubnetMaskList.end(),broadCastAddress) == ipSubnetMaskList.end()) {
			ipSubnetMaskList.push_back(broadCastAddress);
		}
	}

	port = htons( Socket::getBroadCastPort() );

	//for(unsigned int idx = 0; idx < ipList.size() && idx < MAX_NIC_COUNT; idx++) {
	for(unsigned int idx = 0; idx < (unsigned int)ipSubnetMaskList.size(); idx++) {
		// Create the broadcast socket
		memset( &bcLocal[idx], 0, sizeof( struct sockaddr_in));
		bcLocal[idx].sin_family			= AF_INET;
		bcLocal[idx].sin_addr.s_addr	= inet_addr(ipSubnetMaskList[idx].c_str()); //htonl( INADDR_BROADCAST );
		bcLocal[idx].sin_port			= port;  // We are letting the OS fill in the port number for the local machine.
#ifdef WIN32
		bcfd[idx] = INVALID_SOCKET;
#else
		bcfd[idx] = -1;
#endif
		//if(strlen(subnetmask[idx]) > 0) {
		bcfd[idx] = socket( AF_INET, SOCK_DGRAM, 0 );

		if( bcfd[idx] <= 0  ) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Unable to allocate broadcast socket [%s]: %s\n", ipSubnetMaskList[idx].c_str(), Socket::getLastSocketErrorFormattedText().c_str());
			//exit(-1);
		}
		// Mark the socket for broadcast.
		else if( setsockopt( bcfd[idx], SOL_SOCKET, SO_BROADCAST, (const char *) &one, sizeof( int ) ) < 0 ) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Could not set socket to broadcast [%s]: %s\n", ipSubnetMaskList[idx].c_str(), Socket::getLastSocketErrorFormattedText().c_str());
			//exit(-1);
		}
		//}

		if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] setting up broadcast on address [%s]\n",__FILE__,__FUNCTION__,__LINE__,ipSubnetMaskList[idx].c_str());
	}

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	time_t elapsed = 0;
	for( pn = 1; getQuitStatus() == false; pn++ ) {
		for(unsigned int idx = 0; getQuitStatus() == false && idx < ipSubnetMaskList.size(); idx++) {
			if( Socket::isSocketValid(&bcfd[idx]) == true ) {
				try {
					// Send this machine's host name and address in hostname:n.n.n.n format
					snprintf(buff,1024,"%s",myhostname);
					for(unsigned int idx1 = 0; idx1 < ipList.size(); idx1++) {
//						strcat(buff,":");
//						strcat(buff,ipList[idx1].c_str());
//						strcat(buff,":");
//						string port_string = intToStr(this->boundPort);
//#ifdef WIN32
//						strncat(buff,port_string.c_str(),min((int)port_string.length(),100));
//#else
//						strncat(buff,port_string.c_str(),std::min((int)port_string.length(),100));
//#endif
						string buffCopy = buff;
						snprintf(buff,1024,"%s:%s:%d",buffCopy.c_str(),ipList[idx1].c_str(),this->boundPort);
					}

					if(difftime((long int)time(NULL),elapsed) >= 1 && getQuitStatus() == false) {
						elapsed = time(NULL);

						bool pauseBroadCast = getPauseBroadcast();
						if(pauseBroadCast == false) {
							// Broadcast the packet to the subnet
							//if( sendto( bcfd, buff, sizeof(buff) + 1, 0 , (struct sockaddr *)&bcaddr, sizeof(struct sockaddr_in) ) != sizeof(buff) + 1 )

							if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Server is sending broadcast data [%s]\n",buff);

							ssize_t send_res = sendto( bcfd[idx], buff, buffMaxSize, 0 , (struct sockaddr *)&bcLocal[idx], sizeof(struct sockaddr_in) );
							if( send_res != buffMaxSize ) {
								if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Sendto error: %s\n", Socket::getLastSocketErrorFormattedText().c_str());
							}
							else {
								if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcasting on port [%d] the message: [%s]\n",Socket::getBroadCastPort(),buff);
							}
						}
						//printf("Broadcasting server send_res = %d buff [%s] ip [%s] getPauseBroadcast() = %d\n",send_res,buff,ipSubnetMaskList[idx].c_str(),pauseBroadCast);

						if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
					}

					if(getQuitStatus() == true) {
						if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
						break;
					}
					sleep(100); // send out broadcast every 1 seconds
				}
				catch(const exception &ex) {
					SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
					if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
					this->setQuitStatus(true);
				}
				catch(...) {
					SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",__FILE__,__FUNCTION__,__LINE__);
					if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] unknown error\n",__FILE__,__FUNCTION__,__LINE__);
					this->setQuitStatus(true);
				}
			}

			if(getQuitStatus() == true) {
				if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
				break;
			}
		}
		if(getQuitStatus() == true) {
			if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
			break;
		}
	}

	for(unsigned int idx = 0; idx < ipSubnetMaskList.size(); idx++) {
		if( Socket::isSocketValid(&bcfd[idx]) == true ) {
#ifndef WIN32
        ::close(bcfd[idx]);
        bcfd[idx] = -1;
#else
        ::closesocket(bcfd[idx]);
        bcfd[idx] = INVALID_SOCKET;
#endif
		}
	}

	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Broadcast thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
}

double Socket::getAveragePingMS(std::string host, int pingCount) {
	double result = -1;
	return result;

/*
	const bool debugPingOutput = false;
	char szCmd[1024]="";
#ifdef WIN32
	sprintf(szCmd,"ping -n %d %s",pingCount,host.c_str());
	if(debugPingOutput) printf("WIN32 is defined\n");
#elif defined(__GNUC__)
	sprintf(szCmd,"ping -c %d %s",pingCount,host.c_str());
	if(debugPingOutput) printf("NON WIN32 is defined\n");
#else
	#error "Your compiler needs to support popen!"
#endif
	if(szCmd[0] != '\0') {
		FILE *ping= NULL;
#ifdef WIN32
		ping= _popen(szCmd, "r");
		if(debugPingOutput) printf("WIN32 style _popen\n");
#elif defined(__GNUC__)
		ping= popen(szCmd, "r");
		if(debugPingOutput) printf("POSIX style _popen\n");
#else
	#error "Your compiler needs to support popen!"
#endif
		if (ping != NULL){
			char buf[4000]="";
			int bufferPos = 0;
			for(;feof(ping) == false;) {
				char *data = fgets(&buf[bufferPos], 256, ping);
				bufferPos = strlen(buf);
			}
#ifdef WIN32
			_pclose(ping);
#elif defined(__GNUC__)
			pclose(ping);
#else
	#error "Your compiler needs to support popen!"

#endif

			if(debugPingOutput) printf("Running cmd [%s] got [%s]\n",szCmd,buf);

			// Linux
			//softcoder@softhauslinux:~/Code/megaglest/trunk/mk/linux$ ping -c 5 soft-haus.com
			//PING soft-haus.com (65.254.250.110) 56(84) bytes of data.
			//64 bytes from 65-254-250-110.yourhostingaccount.com (65.254.250.110): icmp_seq=1 ttl=242 time=133 ms
			//64 bytes from 65-254-250-110.yourhostingaccount.com (65.254.250.110): icmp_seq=2 ttl=242 time=137 ms
			//
			// Windows XP
			//C:\Code\megaglest\trunk\data\glest_game>ping -n 5 soft-haus.com
			//
			//Pinging soft-haus.com [65.254.250.110] with 32 bytes of data:
			//
			//Reply from 65.254.250.110: bytes=32 time=125ms TTL=242
			//Reply from 65.254.250.110: bytes=32 time=129ms TTL=242

			std::string str = buf;
			std::string::size_type ms_pos = 0;
			int count = 0;
			while ( ms_pos != std::string::npos) {
				ms_pos = str.find("time=", ms_pos);

				if(debugPingOutput) printf("count = %d ms_pos = %d\n",count,ms_pos);

				if ( ms_pos != std::string::npos ) {
					++count;

					int endPos = str.find(" ms", ms_pos+5 );

					if(debugPingOutput) printf("count = %d endPos = %d\n",count,endPos);

					if(endPos == std::string::npos) {
						endPos = str.find("ms ", ms_pos+5 );

						if(debugPingOutput) printf("count = %d endPos = %d\n",count,endPos);
					}

					if(endPos != std::string::npos) {

						if(count == 1) {
							result = 0;
						}
						int startPos = ms_pos + 5;
						int posLength = endPos - startPos;
						if(debugPingOutput) printf("count = %d startPos = %d posLength = %d str = [%s]\n",count,startPos,posLength,str.substr(startPos, posLength).c_str());

						float pingMS = strToFloat(str.substr(startPos, posLength));
						result += pingMS;
					}

					ms_pos += 5; // start next search after this "time="
				}
			}

			if(result > 0 && count > 1) {
				result /= count;
			}
		}
	}
	return result;
*/
}

std::string Socket::getIpAddress() {
	return ipAddress;
}

void ServerSocket::addIPAddressToBlockedList(string value) {
	if(isIPAddressBlocked(value) == false) {
		blockIPList.push_back(value);
		if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Blocked IP Address [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,value.c_str());
	}
}
bool ServerSocket::isIPAddressBlocked(string value) const {
	bool result = (std::find(blockIPList.begin(),blockIPList.end(),value) != blockIPList.end());
	return result;
}

void ServerSocket::removeBlockedIPAddress(string value) {
	vector<string>::iterator iterFind = std::find(blockIPList.begin(),blockIPList.end(),value);
	if(iterFind != blockIPList.end()) {
		blockIPList.erase(iterFind);
	}
}

void ServerSocket::clearBlockedIPAddress() {
	blockIPList.clear();
}

bool ServerSocket::hasBlockedIPAddresses() const {
	return(blockIPList.size() > 0);
}

}}//end namespace