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

#include <config.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/times.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

#include <netdb.h>

#include <stdlib.h>
#include <unistd.h>

#include <tqglobal.h>
#include <tqstring.h>
#include <tqiodevice.h>
#include <tqsocketnotifier.h>
#include <tqguardedptr.h>

#include "kresolver.h"

#include "kdebug.h"
#include "kextsock.h"
#include "ksockaddr.h"
#include "ksocks.h"

#ifdef __CYGWIN__
#include "netsupp.h"
#endif 

using namespace KNetwork;

//
// Internal class definitions
//

class KExtendedSocketPrivate
{
public:
  int flags;			// socket flags
  int status;			// status
  int syserror;			// the system error value

  timeval timeout;		// connection/acception timeout

  KResolver resRemote;		// the resolved addresses
  KResolver resLocal;		// binding resolution
  unsigned current;		// used by the asynchronous connection

  ::KSocketAddress *local;	// local socket address
  ::KSocketAddress *peer;	// peer socket address

  TQSocketNotifier *qsnIn, *qsnOut;
  int inMaxSize, outMaxSize;
  bool emitRead : 1, emitWrite : 1;
  mutable bool addressReusable : 1, ipv6only : 1;

  KExtendedSocketPrivate() :
    flags(0), status(0), syserror(0),
    current(0), local(0), peer(0),
    qsnIn(0), qsnOut(0), inMaxSize(-1), outMaxSize(-1), emitRead(false), emitWrite(false),
    addressReusable(false), ipv6only(false)
  {
    timeout.tv_sec = timeout.tv_usec = 0;
  }
};

// translate KExtendedSocket flags into KResolver ones
static bool process_flags(int flags, int& socktype, int& familyMask, int& outflags)
{
  switch (flags & (KExtendedSocket::streamSocket | KExtendedSocket::datagramSocket | KExtendedSocket::rawSocket))
    {
    case 0:
      /* No flags given, use default */

    case KExtendedSocket::streamSocket:
      /* streaming socket requested */
      socktype = SOCK_STREAM;
      break;

    case KExtendedSocket::datagramSocket:
      /* datagram packet socket requested */
      socktype = SOCK_DGRAM;
      break;

    case KExtendedSocket::rawSocket:
      /* raw socket requested. I wouldn't do this if I were you... */
      socktype = SOCK_RAW;
      break;

    default:
      /* the flags were used in an invalid manner */
      return false;
    }

  if (flags & KExtendedSocket::knownSocket)
    {
      familyMask = 0;
      if ((flags & KExtendedSocket::unixSocket) == KExtendedSocket::unixSocket)
	familyMask |= KResolver::UnixFamily;

      switch ((flags & (KExtendedSocket::ipv6Socket|KExtendedSocket::ipv4Socket)))
	{
	case KExtendedSocket::ipv4Socket:
	  familyMask |= KResolver::IPv4Family;
	  break;
	case KExtendedSocket::ipv6Socket:
	  familyMask |= KResolver::IPv6Family;
	  break;
	case KExtendedSocket::inetSocket:
	  familyMask |= KResolver::InternetFamily;
	  break;
	}

      // those are all the families we know about
    }
  else
    familyMask = KResolver::KnownFamily;

  /* check other flags */
  outflags = (flags & KExtendedSocket::passiveSocket ? KResolver::Passive : 0) |
    (flags & KExtendedSocket::canonName ? KResolver::CanonName : 0) |
    (flags & KExtendedSocket::noResolve ? KResolver::NoResolve : 0);

  if (getenv("KDE_NO_IPV6"))
    familyMask &= ~KResolver::IPv6Family;

  return true;
}

// "skips" at most len bytes from file descriptor fd
// that is, we will try and read that much data and discard
// it. We will stop when we have read those or when the read
// function returns error
static int skipData(int fd, unsigned len)
{
  char buf[1024];
  unsigned skipped = 0;
  while (len)
    {
      int count = sizeof(buf);
      if ((unsigned)count > len)
	count = len;
      count = KSocks::self()->read(fd, buf, count);
      if (count == -1)
	return -1;
      else
	{
	  len -= count;
	  skipped += count;
	}
    }
  return skipped;
}

/*
 * class KExtendedSocket
 */

// default constructor
KExtendedSocket::KExtendedSocket() :
  sockfd(-1), d(new KExtendedSocketPrivate)
{
}

// constructor with hostname
KExtendedSocket::KExtendedSocket(const TQString& host, int port, int flags) :
  sockfd(-1), d(new KExtendedSocketPrivate)
{
  setAddress(host, port);
  setSocketFlags(flags);
}

// same
KExtendedSocket::KExtendedSocket(const TQString& host, const TQString& service, int flags) :
  sockfd(-1), d(new KExtendedSocketPrivate)
{
  setAddress(host, service);
  setSocketFlags(flags);
}

// destroy the class
KExtendedSocket::~KExtendedSocket()
{
  closeNow();

  if (d->local != NULL)
    delete d->local;
  if (d->peer != NULL)
    delete d->peer;

  if (d->qsnIn != NULL)
    delete d->qsnIn;
  if (d->qsnOut != NULL)
    delete d->qsnOut;

  delete d;
}

#ifdef USE_QT3
void KExtendedSocket::reset()
#endif // USE_QT3
#ifdef USE_QT4
bool KExtendedSocket::reset()
#endif // USE_QT4
{
  closeNow();
  release();
  d->current = 0;
  d->status = nothing;
  d->syserror = 0;
}

int KExtendedSocket::socketStatus() const
{
  return d->status;
}

void KExtendedSocket::setSocketStatus(int newstatus)
{
  d->status = newstatus;
}

void KExtendedSocket::setError(int errorcode, int syserror)
{
  setqStatus(errorcode);
  d->syserror = syserror;
}

int KExtendedSocket::systemError() const
{
  return d->syserror;
}

/*
 * Sets socket flags
 * This is only allowed if we are in nothing state
 */
int KExtendedSocket::setSocketFlags(int flags)
{
  if (d->status > nothing)
    return -1;			// error!

  return d->flags = flags;
}

int KExtendedSocket::socketFlags() const
{
  return d->flags;
}

/*
 * Sets socket target hostname
 * This is only allowed if we are in nothing state
 */
bool KExtendedSocket::setHost(const TQString& host)
{
  if (d->status > nothing)
    return false;		// error!

  d->resRemote.setNodeName(host);
  return true;
}

/*
 * returns the hostname
 */
TQString KExtendedSocket::host() const
{
  return d->resRemote.nodeName();
}

/*
 * Sets the socket target port/service
 * Same thing: only state 'nothing'
 */
bool KExtendedSocket::setPort(int port)
{
  return setPort(TQString::number(port));
}

bool KExtendedSocket::setPort(const TQString& service)
{
  if (d->status > nothing)
    return false;		// error

  d->resRemote.setServiceName(service);
  return true;
}

/*
 * returns the service port number
 */
TQString KExtendedSocket::port() const
{
  return d->resRemote.serviceName();
}

/*
 * sets the address
 */
bool KExtendedSocket::setAddress(const TQString& host, int port)
{
  return setHost(host) && setPort(port);
}

/*
 * the same
 */
bool KExtendedSocket::setAddress(const TQString& host, const TQString& serv)
{
  return setHost(host) && setPort(serv);
}

/*
 * Sets the bind hostname
 * This is only valid in the 'nothing' state and if this is not a
 * passiveSocket socket
 */
bool KExtendedSocket::setBindHost(const TQString& host)
{
  if (d->status > nothing || d->flags & passiveSocket)
    return false;		// error

  d->resLocal.setServiceName(host);
  return true;
}

/*
 * Unsets the bind hostname
 * same thing
 */
bool KExtendedSocket::unsetBindHost()
{
  return setBindHost(TQString::null);
}

/*
 * returns the binding host
 */
TQString KExtendedSocket::bindHost() const
{
  return d->resLocal.serviceName();
}

/*
 * Sets the bind port
 * Same condition as setBindHost
 */
bool KExtendedSocket::setBindPort(int port)
{
  return setBindPort(TQString::number(port));
}

bool KExtendedSocket::setBindPort(const TQString& service)
{
  if (d->status > nothing || d->flags & passiveSocket)
    return false;		// error

  d->resLocal.setServiceName(service);
  return true;
}

/*
 * unsets the bind port
 */
bool KExtendedSocket::unsetBindPort()
{
  return setBindPort(TQString::null);
}

/*
 * returns the binding port
 */
TQString KExtendedSocket::bindPort() const
{
  return d->resLocal.serviceName();
}

/*
 * sets the binding address
 */
bool KExtendedSocket::setBindAddress(const TQString& host, int port)
{
  return setBindHost(host) && setBindPort(port);
}

/*
 * same
 */
bool KExtendedSocket::setBindAddress(const TQString& host, const TQString& service)
{
  return setBindHost(host) && setBindPort(service);
}

/*
 * unsets binding address
 */
bool KExtendedSocket::unsetBindAddress()
{
  return unsetBindHost() && unsetBindPort();
}

/*
 * sets the timeout for the connection
 */
bool KExtendedSocket::setTimeout(int secs, int usecs)
{
  if (d->status >= connected)	// closed?
    return false;

  d->timeout.tv_sec = secs;
  d->timeout.tv_usec = usecs;
  return true;
}

/*
 * returns the timeout
 */
timeval KExtendedSocket::timeout() const
{
  return d->timeout;
}

/*
 * Sets the blocking mode on this socket
 */
bool KExtendedSocket::setBlockingMode(bool enable)
{
  cleanError();
  if (d->status < created)
    return false;

  if (sockfd == -1)
    return false;		// error!

  int fdflags = fcntl(sockfd, F_GETFL, 0);
  if (fdflags == -1)
    return false;		// error!

  if (!enable)
    fdflags |= O_NONBLOCK;
  else
    fdflags &= ~O_NONBLOCK;

  if (fcntl(sockfd, F_SETFL, fdflags) == -1)
    {
      setError(IO_UnspecifiedError, errno);
      return false;
    }
  return true;
}

/*
 * Returns the blocking mode on the socket
 */
bool KExtendedSocket::blockingMode()
{
  cleanError();
  if (d->status < created)
    return false;		// sockets not created are in blocking mode

  if (sockfd == -1)
    return false;		// error

  int fdflags = fcntl(sockfd, F_GETFL, 0);
  if (fdflags == -1)
    {
      setError(IO_UnspecifiedError, errno);
      return false;
    }
  return (fdflags & O_NONBLOCK) == 0; // non-blocking == false
}

/*
 * Sets the reusability flag for this socket in the OS
 */
bool KExtendedSocket::setAddressReusable(bool enable)
{
  cleanError();
  d->addressReusable = enable;
  if (d->status < created)
    return true;

  if (sockfd == -1)
    return true;

  if (!setAddressReusable(sockfd, enable))
    {
      setError(IO_UnspecifiedError, errno);
      return false;
    }
  return true;
}

bool KExtendedSocket::setAddressReusable(int fd, bool enable)
{
  if (fd == -1)
    return false;

  int on = enable;		// just to be on the safe side

  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) == -1)
    return false;
  return true;
}

/*
 * Retrieves the reusability flag for this socket
 */
bool KExtendedSocket::addressReusable()
{
  cleanError();
  if (d->status < created)
    return d->addressReusable;

  if (sockfd == -1)
    return d->addressReusable;

  int on;
  socklen_t onsiz = sizeof(on);
  if (getsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, &onsiz) == -1)
    {
      setError(IO_UnspecifiedError, errno);
      return false;
    }

  return on != 0;
}

/*
 * Set the IPV6_V6ONLY flag
 */
bool KExtendedSocket::setIPv6Only(bool enable)
{
#ifdef IPV6_V6ONLY
  cleanError();

  d->ipv6only = enable;
  if (sockfd == -1)
    return true;		// can't set on a non-existing socket

  int on = enable;

  if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
		 (char *)&on, sizeof(on)) == -1)
    {
      setError(IO_UnspecifiedError, errno);
      return false;
    }
  else
    return true;

#else
  // we don't have the IPV6_V6ONLY constant in this system
  d->ipv6only = enable;

  setError(IO_UnspecifiedError, ENOSYS);
  return false;			// can't set if we don't know about this flag
#endif
}

/*
 * retrieve the IPV6_V6ONLY flag
 */
bool KExtendedSocket::isIPv6Only()
{
#ifdef IPV6_V6ONLY
  cleanError();

  if (d->status < created || sockfd == -1)
    return d->ipv6only;

  int on;
  socklen_t onsiz = sizeof(on);
  if (getsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
		 (char *)&on, &onsiz) == -1)
    {
      setError(IO_UnspecifiedError, errno);
      return false;
    }

  return d->ipv6only = on;

#else
  // we don't have the constant
  setError(IO_UnspecifiedError, ENOSYS);
  return false;
#endif
}

/*
 * Sets the buffer sizes in this socket
 * Also, we create or delete the socket notifiers
 */
bool KExtendedSocket::setBufferSize(int rsize, int wsize)
{
  cleanError();
  if (d->status < created)
    return false;

  if (sockfd == -1)
    return false;

  if (d->flags & passiveSocket)
    return false;		// no I/O on passive sockets

  if (rsize < -2)
    return false;

  if (wsize < -2)
    return false;

  // LOCK BUFFER MUTEX

  // The input socket notifier is always enabled
  // That happens because we want to be notified of when the socket gets
  // closed
  if (d->qsnIn == NULL)
    {
      d->qsnIn = new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
      TQObject::connect(d->qsnIn, TQT_SIGNAL(activated(int)), this, TQT_SLOT(socketActivityRead()));
      d->qsnIn->setEnabled(true);
    }

  if (rsize == 0 && d->flags & inputBufferedSocket)
    {
      // user wants to disable input buffering
      d->flags &= ~inputBufferedSocket;

      consumeReadBuffer(readBufferSize(), NULL, true);
      d->inMaxSize = 0;
    }
  else if (rsize != -2)
    {
      // enabling input buffering
      if (rsize)
	d->flags |= inputBufferedSocket;
      d->inMaxSize = rsize;

      if (rsize > 0 && (unsigned)rsize < readBufferSize())
	// input buffer has more data than the new size; discard
	consumeReadBuffer(readBufferSize() - rsize, NULL, true);

    }

  if (wsize == 0 && d->flags & outputBufferedSocket)
    {
      // disabling output buffering
      d->flags &= ~outputBufferedSocket;
      if (d->qsnOut && !d->emitWrite)
	d->qsnOut->setEnabled(false);
      consumeWriteBuffer(writeBufferSize());
      d->outMaxSize = 0;
    }
  else if (wsize != -2)
    {
      // enabling input buffering
      if (wsize)
	d->flags |= outputBufferedSocket;
      d->outMaxSize = wsize;

      if (wsize > 0 && (unsigned)wsize < writeBufferSize())
	// output buffer is bigger than it is to become; shrink
	consumeWriteBuffer(writeBufferSize() - wsize);

      if (d->qsnOut == NULL)
	{
	  d->qsnOut = new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
	  TQObject::connect(d->qsnOut, TQT_SIGNAL(activated(int)), this, TQT_SLOT(socketActivityWrite()));
	  // if the class is being created now, there's nothing to write yet
	  // so socketActivityWrite() will get called once and disable
	  // the notifier
	}
    }

  // UNLOCK BUFFER MUTEX

  setFlags((mode() & ~IO_Raw) | ((d->flags & bufferedSocket) ? 0 : IO_Raw));

  // check we didn't turn something off we shouldn't
  if (d->emitWrite && d->qsnOut == NULL)
    {
      d->qsnOut = new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
      TQObject::connect(d->qsnOut, TQT_SIGNAL(activated(int)), this, TQT_SLOT(socketActivityWrite()));
    }

  return true;
}

/*
 * Finds the local address for this socket
 * if we have done this already, we return it. Otherwise, we'll have
 * to find the socket name
 */
const ::KSocketAddress *KExtendedSocket::localAddress()
{
  if (d->local != NULL)
    return d->local;
  if (d->status < bound)
    return NULL;

  return d->local = localAddress(sockfd);
}

/*
 * Same thing, but for peer address. Which means this does not work on
 * passiveSocket and that we require to be connected already. Also note that
 * the behavior on connectionless sockets is not defined here.
 */
const ::KSocketAddress* KExtendedSocket::peerAddress()
{
  if (d->peer != NULL)
    return d->peer;
  if (d->flags & passiveSocket || d->status < connected)
    return NULL;

  return d->peer = peerAddress(sockfd);
}

/*
 * Perform the lookup on the addresses given
 */
int KExtendedSocket::lookup()
{
  if (startAsyncLookup() != 0)
    return -1;

  if (!d->resRemote.wait() || !d->resLocal.wait())
    {
      d->status = nothing;
      return -1;
    }

  d->status = lookupDone;
  if (d->resRemote.error() != KResolver::NoError)
    return d->resRemote.error();
  if (d->resLocal.error() != KResolver::NoError)
    return d->resLocal.error();
  return 0;
}

/*
 * Performs an asynchronous lookup on the given address(es)
 */
int KExtendedSocket::startAsyncLookup()
{
  cleanError();
  if (d->status > lookupInProgress)
    return -1;
  if (d->status == lookupInProgress)
    // already in progress
    return 0;

  /* check socket type flags */
  int socktype, familyMask, flags;
  if (!process_flags(d->flags, socktype, familyMask, flags))
    return -2;

  // perform the global lookup before
  if (!d->resRemote.isRunning())
    {
      d->resRemote.setFlags(flags);
      d->resRemote.setFamily(familyMask);
      d->resRemote.setSocketType(socktype);
      TQObject::connect(&d->resRemote, TQT_SIGNAL(finished(KResolverResults)), 
		       this, TQT_SLOT(dnsResultsReady()));

      if (!d->resRemote.start())
	{
	  setError(IO_LookupError, d->resRemote.error());
	  return d->resRemote.error();
	}
    }

  if ((d->flags & passiveSocket) == 0 && !d->resLocal.isRunning())
    {
      /* keep flags, but make this passive */
      flags |= KResolver::Passive;
      d->resLocal.setFlags(flags);
      d->resLocal.setFamily(familyMask);
      d->resLocal.setSocketType(socktype);
      TQObject::connect(&d->resLocal, TQT_SIGNAL(finished(KResolverResults)), 
		       this, TQT_SLOT(dnsResultsReady()));

      if (!d->resLocal.start())
	{
	  setError(IO_LookupError, d->resLocal.error());
	  return d->resLocal.error();
	}
    }

  // if we are here, there were no errors
  if (d->resRemote.isRunning() || d->resLocal.isRunning())
    d->status = lookupInProgress; // only if there actually is a running lookup
  else
    {
      d->status = lookupDone;
      emit lookupFinished(d->resRemote.results().count() + 
			  d->resLocal.results().count());
    }
  return 0;
}

void KExtendedSocket::cancelAsyncLookup()
{
  cleanError();
  if (d->status != lookupInProgress)
    return;			// what's to cancel?

  d->status = nothing;
  d->resLocal.cancel(false);
  d->resRemote.cancel(false);
}

int KExtendedSocket::listen(int N)
{
  cleanError();
  if ((d->flags & passiveSocket) == 0 || d->status >= listening)
    return -2;
  if (d->status < lookupDone)
    if (lookup() != 0)
      return -2;		// error!
  if (d->resRemote.error())
    return -2;
  
  // doing the loop:
  KResolverResults::const_iterator it;
  KResolverResults res = d->resRemote.results();
  for (it = res.begin(); it != res.end(); ++it)
    {
      //kdDebug(170) << "Trying to listen on " << (*it).address().toString() << endl;
      sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
      if (sockfd == -1)
	{
	  // socket failed creating
	  //kdDebug(170) << "Failed to create: " << perror << endl;
	  continue;
	}
	
      fcntl(sockfd, F_SETFD, FD_CLOEXEC);

      if (d->addressReusable)
	setAddressReusable(sockfd, true);
      setIPv6Only(d->ipv6only);
      cleanError();
      if (KSocks::self()->bind(sockfd, (*it).address().address(), (*it).length()) == -1)
	{
	  //kdDebug(170) << "Failed to bind: " << perror << endl;
	  ::close(sockfd);
	  sockfd = -1;
	  continue;
	}

      // ok, socket has bound
      // kdDebug(170) << "Socket bound: " << sockfd << endl;

      d->status = bound;
      break;
    }

  if (sockfd == -1)
    {
      setError(IO_ListenError, errno);
      //kdDebug(170) << "Listen error - sockfd is -1 " << endl;
      return -1;
    }

  d->status = bound;
  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);

  int retval = KSocks::self()->listen(sockfd, N);
  if (retval == -1)
    setError(IO_ListenError, errno);
  else
    {
      d->status = listening;
      d->qsnIn = new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
      TQObject::connect(d->qsnIn, TQT_SIGNAL(activated(int)), this, TQT_SLOT(socketActivityRead()));
    }
  return retval == -1 ? -1 : 0;
}

int KExtendedSocket::accept(KExtendedSocket *&sock)
{
  cleanError();
  sock = NULL;
  if ((d->flags & passiveSocket) == 0 || d->status >= accepting)
    return -2;
  if (d->status < listening)
    if (listen() < 0)
      return -2;		// error!

  // let's see
  // if we have a timeout in place, we have to place this socket in non-blocking
  // mode
  bool block = blockingMode();
  struct sockaddr sa;
  ksocklen_t len = sizeof(sa);
  sock = NULL;

  if (d->timeout.tv_sec > 0 || d->timeout.tv_usec > 0)
    {
      fd_set set;

      setBlockingMode(false);	// turn on non-blocking
      FD_ZERO(&set);
      FD_SET(sockfd, &set);

      //kdDebug(170).form("Accepting on %d with %d.%06d second timeout\n",
      //	     sockfd, d->timeout.tv_sec, d->timeout.tv_usec);
      // check if there is anything to accept now
      int retval = KSocks::self()->select(sockfd + 1, &set, NULL, NULL, &d->timeout);
      if (retval == -1)
	{
	  setError(IO_UnspecifiedError, errno);
	  return -1;		// system error
	}
      else if (retval == 0 || !FD_ISSET(sockfd, &set))
	{
	  setError(IO_TimeOutError, 0);
	  return -3;		// timeout
	}
    }

  // it's common stuff here
  int newfd = KSocks::self()->accept(sockfd, &sa, &len);

  if (newfd == -1)
    {
      setError(IO_AcceptError, errno);
      kdWarning(170) << "Error accepting on socket " << sockfd << ":"
		     << perror << endl;
      return -1;
    }

  fcntl(newfd, F_SETFD, FD_CLOEXEC);

  //kdDebug(170).form("Socket %d accepted socket %d\n", sockfd, newfd);

  setBlockingMode(block);	// restore blocking mode

  sock = new KExtendedSocket;
  sock->d->status = connected;
  sock->sockfd = newfd;
  sock->setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
  sock->setBufferSize(0, 0);	// always unbuffered here. User can change that later

  return 0;
}

/*
 * tries to connect
 *
 * FIXME!
 * This function is critical path. It has to be cleaned up and made faster
 */
int KExtendedSocket::connect()
{
  cleanError();
  if (d->flags & passiveSocket || d->status >= connected)
    return -2;
  if (d->status < lookupDone)
    if (lookup() != 0)
      return -2;

  timeval end, now;
  timeval timeout_copy = d->timeout;
  // Ok, things are a little tricky here
  // Let me explain
  // getaddrinfo() will return several different families of sockets
  // When we have to bind before we connect, we have to make sure we're binding
  // and connecting to the same family, or things won't work

  KResolverResults remote = d->resRemote.results(),
    local = d->resLocal.results();
  KResolverResults::const_iterator it, it2;
  //kdDebug(170) << "Starting connect to " << host() << '|' << port() 
  //             << ": have " << local.count() << " local entries and "
  //             << remote.count() << " remote" << endl;

  int ret = -1;
  for (it = remote.begin(), it2 = local.begin(); it != remote.end(); ++it)
    {
      bool doingtimeout = d->timeout.tv_sec > 0 || d->timeout.tv_usec > 0;
      if (doingtimeout)
        {
          gettimeofday(&end, NULL);
          end.tv_usec += d->timeout.tv_usec;
          end.tv_sec += d->timeout.tv_sec;
          if (end.tv_usec > 1000*1000)
            {
              end.tv_usec -= 1000*1000;
              end.tv_sec++;
            }
          //kdDebug(170).form("Connection with timeout of %d.%06d seconds (ends in %d.%06d)\n",
          //    d->timeout.tv_sec, d->timeout.tv_usec, end.tv_sec, end.tv_usec);
        }

      //kdDebug(170) << "Trying to connect to " << (*it).address().toString() << endl;
      if (it2 != local.end())
	{
//	  //kdDebug(170) << "Searching bind socket for family " << p->ai_family << endl;
	  if ((*it).family() != (*it2).family())
	    // differing families, scan local for a matching family
	    for (it2 = local.begin(); it2 != local.end(); ++it2)
	      if ((*it).family() == (*it2).family())
		break;

	  if ((*it).family() != (*it2).family())
	    {
	      // no matching families for this
	      //kdDebug(170) << "No matching family for bind socket\n";
	      it2 = local.begin();
	      continue;
	    }

	  //kdDebug(170) << "Binding on " << (*it2).address().toString() << " before connect" << endl;
	  errno = 0;
	  sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
	  setError(IO_ConnectError, errno);
	  if (sockfd == -1)
	    continue;		// cannot create this socket
          fcntl(sockfd, F_SETFD, FD_CLOEXEC);
	  if (d->addressReusable)
	    setAddressReusable(sockfd, true);
	  setIPv6Only(d->ipv6only);
	  cleanError();
	  if (KSocks::self()->bind(sockfd, (*it2).address(), (*it2).length()))
	    {
	      //kdDebug(170) << "Bind failed: " << perror << endl;
	      ::close(sockfd);
	      sockfd = -1;
	      continue;
	    }
	}
      else
	{
	  // no need to bind, just create
	  sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
	  if (sockfd == -1)
	    {
	      setError(IO_ConnectError, errno);
	      continue;
	    }
          fcntl(sockfd, F_SETFD, FD_CLOEXEC);
	  if (d->addressReusable)
	    setAddressReusable(sockfd, true);
	  setIPv6Only(d->ipv6only);
	  cleanError();
	}

//      kdDebug(170) << "Socket " << sockfd << " created" << endl;
      d->status = created;

      // check if we have to do timeout
      if (doingtimeout && KSocks::self()->hasWorkingAsyncConnect())
	{
	  fd_set rd, wr;

	  setBlockingMode(false);

	  // now try and connect
	  if (KSocks::self()->connect(sockfd, (*it).address(), (*it).length()) == -1)
	    {
	      // this could be EWOULDBLOCK
	      if (errno != EWOULDBLOCK && errno != EINPROGRESS)
		{
		  //kdDebug(170) << "Socket " << sockfd << " did not connect: " << perror << endl;
		  setError(IO_ConnectError, errno);
		  ::close(sockfd);
		  sockfd = -1;
		  continue;	// nope, another error
		}

	      FD_ZERO(&rd);
	      FD_ZERO(&wr);
	      FD_SET(sockfd, &rd);
	      FD_SET(sockfd, &wr);

	      int retval = KSocks::self()->select(sockfd + 1, &rd, &wr, NULL, &d->timeout);
	      if (retval == -1)
		{
		  setError(IO_FatalError, errno);
		  continue;	// system error
		}
	      else if (retval == 0)
		{
		  ::close(sockfd);
		  sockfd = -1;
//		  kdDebug(170) << "Time out while trying to connect to " <<
//		    (*it).address().toString() << endl;
		  setError(IO_TimeOutError, 0);
		  ret = -3;	// time out

                  d->timeout.tv_usec += timeout_copy.tv_usec;
                  d->timeout.tv_sec  += timeout_copy.tv_sec;
                  if (d->timeout.tv_usec < 0)
                    {
                      d->timeout.tv_usec += 1000*1000;
                      d->timeout.tv_sec--;
                    }

		  continue;
		}

	      // adjust remaining time
	      gettimeofday(&now, NULL);
	      d->timeout.tv_sec = end.tv_sec - now.tv_sec;
	      d->timeout.tv_usec = end.tv_usec - now.tv_usec;
	      if (d->timeout.tv_usec < 0)
		{
		  d->timeout.tv_usec += 1000*1000;
		  d->timeout.tv_sec--;
		}
//	      kdDebug(170).form("Socket %d activity; %d.%06d seconds remaining\n",
//			     sockfd, d->timeout.tv_sec, d->timeout.tv_usec);

	      // this means that an event occurred in the socket
	      int errcode;
	      socklen_t len = sizeof(errcode);
	      retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char*)&errcode,
				  &len);
	      if (retval == -1 || errcode != 0)
		{
		  // socket did not connect
		  //kdDebug(170) << "Socket " << sockfd << " did not connect: "
		  //	    << strerror(errcode) << endl;
		  ::close(sockfd);
		  sockfd = -1;

		  // this is HIGHLY UNLIKELY
		  if (d->timeout.tv_sec == 0 && d->timeout.tv_usec == 0)
		    {
		      d->status = lookupDone;
		      setError(IO_TimeOutError, 0);
		      return -3; // time out
		    }

		  setError(IO_ConnectError, errcode);
		  continue;
		}
	    }

	  // getting here means it connected
	  // setBufferSize() takes care of creating the socket notifiers
	  setBlockingMode(true);
	  d->status = connected;
	  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
	  setBufferSize(d->flags & inputBufferedSocket ? -1 : 0,
			d->flags & outputBufferedSocket ? -1 : 0);
	  emit connectionSuccess();
//	  kdDebug(170) << "Socket " << sockfd << " connected\n";
	  return 0;
	}
      else
	{
	  // without timeouts
	  if (KSocks::self()->connect(sockfd, (*it).address(), (*it).length()) == -1)
	    {
	      //kdDebug(170) << "Socket " << sockfd << " to " << (*it).address().toString() 
	      //	   << " did not connect: " << perror << endl;
	      setError(IO_ConnectError, errno);
	      ::close(sockfd);
	      sockfd = -1;
	      continue;
	    }

	  d->status = connected;
	  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
	  setBufferSize(d->flags & inputBufferedSocket ? -1 : 0,
			d->flags & outputBufferedSocket ? -1 : 0);
	  emit connectionSuccess();
//	  kdDebug(170) << "Socket " << sockfd << " connected\n";
	  return 0;		// it connected
	}
    }

  // getting here means no socket connected or stuff like that
  emit connectionFailed(d->syserror);
  //kdDebug(170) << "Failed to connect\n";
  return ret;
}

int KExtendedSocket::startAsyncConnect()
{
  cleanError();
  // check status
  if (d->status >= connected || d->flags & passiveSocket)
    return -2;

  if (d->status == connecting)
    // already on async connect
    return 0;

  // check if we have to do lookup
  // if we do, then we'll use asynchronous lookup and use
  // signal lookupFinished to do connection
  if (d->status < lookupDone)
    {
      TQObject::connect(this, TQT_SIGNAL(lookupFinished(int)), this, TQT_SLOT(startAsyncConnectSlot()));
      if (d->status < lookupInProgress)
	return startAsyncLookup();
      else
	return 0;		// we still have to wait
    }

  // here we have d->status >= lookupDone and <= connecting
  // we can do our connection
  d->status = connecting;
  TQGuardedPtr<TQObject> p = TQT_TQOBJECT(this);
  connectionEvent();
  if (!p) 
    return -1; // We have been deleted.
  if (d->status < connecting)
    return -1;
  return 0;
}

void KExtendedSocket::cancelAsyncConnect()
{
  if (d->status != connecting)
    return;

  if (sockfd != -1)
    {
      // we have a waiting connection
      if (d->qsnIn)
	delete d->qsnIn;
      if (d->qsnOut)
	delete d->qsnOut;
      d->qsnIn = d->qsnOut = NULL;

      ::close(sockfd);
      sockfd = -1;
    }
  d->status = lookupDone;
}

bool KExtendedSocket::open(TQ_OpenMode mode)
{
  if (mode != IO_Raw | IO_ReadWrite)
    return false;		// invalid open mode

  if (d->flags & passiveSocket)
    return listen() == 0;
  else if (d->status < connecting)
    return connect() == 0;
  else
    return false;
}

void KExtendedSocket::close()
{
  if (sockfd == -1 || d->status >= closing)
    return;			// nothing to close

  // LOCK BUFFER MUTEX
  if (d->flags & outputBufferedSocket && writeBufferSize() > 0)
    {
      // write buffer not empty, go into closing state
      d->status = closing;
      if (d->qsnIn)
	delete d->qsnIn;
      d->qsnIn = NULL;
      // we keep the outgoing socket notifier because we want
      // to send data, but not receive
    }
  else
    {
      // nope, write buffer is empty
      // we can close now
      if (d->qsnIn)
	delete d->qsnIn;
      if (d->qsnOut)
	delete d->qsnOut;
      d->qsnIn = d->qsnOut = NULL;

      ::close(sockfd);
      d->status = done;
      emit closed(readBufferSize() != 0 ? availRead : 0);
    }
  // UNLOCK BUFFER MUTEX
}


void KExtendedSocket::closeNow()
{
  if (d->status >= done)
    return;			// nothing to close

  // close the socket
  delete d->qsnIn;
  delete d->qsnOut;
  d->qsnIn = d->qsnOut = NULL;

  if (d->status > connecting && sockfd != -1)
    {
      ::close(sockfd);
      sockfd = -1;
    }
  else if (d->status == connecting)
    cancelAsyncConnect();
  else if (d->status == lookupInProgress)
    cancelAsyncLookup();

  d->status = done;

  emit closed(closedNow |
	      (readBufferSize() != 0 ? availRead : 0) |
	      (writeBufferSize() != 0 ? dirtyWrite : 0));
}

void KExtendedSocket::release()
{
  // release our hold on the socket
  sockfd = -1;
  d->status = done;

  d->resRemote.cancel(false);
  d->resLocal.cancel(false);

  if (d->local != NULL)
    delete d->local;
  if (d->peer != NULL)
    delete d->peer;

  d->peer = d->local = NULL;

  if (d->qsnIn != NULL)
    delete d->qsnIn;
  if (d->qsnOut != NULL)
    delete d->qsnOut;

  d->qsnIn = d->qsnOut = NULL;

  // now that the socket notificators are done with, we can flush out the buffers
  consumeReadBuffer(readBufferSize(), NULL, true);
  consumeWriteBuffer(writeBufferSize());

  // don't delete d
  // leave that for the destructor
}

void KExtendedSocket::flush()
{
  cleanError();
  if (d->status < connected || d->status >= done || d->flags & passiveSocket)
    return;

  if (sockfd == -1)
    return;

  if ((d->flags & outputBufferedSocket) == 0)
    return;			// nothing to do

  // LOCK MUTEX

  unsigned written = 0;
  unsigned offset = outBufIndex; // this happens only for the first
  while (writeBufferSize() - written > 0)
    {
      // we have to write each output buffer in outBuf
      // but since we can have several very small buffers, we can make things
      // better by concatenating a few of them into a big buffer
      // question is: how big should that buffer be? 16 kB should be enough

      TQByteArray buf(16384);
      TQByteArray *a = outBuf.first();
      unsigned count = 0;

      while (a && count + (a->size() - offset) <= buf.size())
	{
	  memcpy(buf.data() + count, a->data() + offset, a->size() - offset);
	  count += a->size() - offset;
	  offset = 0;
	  a = outBuf.next();
	}

      // see if we can still fit more
      if (a && count < buf.size())
	{
	  // getting here means this buffer (a) is larger than
	  // (buf.size() - count) (even for count == 0).
	  memcpy(buf.data() + count, a->data() + offset, buf.size() - count);
	  offset += buf.size() - count;
	  count = buf.size();
	}

      // now try to write those bytes
      int wrote = KSocks::self()->write(sockfd, buf, count);

      if (wrote == -1)
	{
	  // could be EAGAIN (EWOULDBLOCK)
	  setError(IO_WriteError, errno);
	  break;
	}
      written += wrote;

      if ((unsigned)wrote != count)
	break;
    }
  if (written)
    {
      consumeWriteBuffer(written);
      emit bytesWritten(written);
    }

  // UNLOCK MUTEX
}


TQT_TQIO_LONG KExtendedSocket::tqreadBlock(char *data, TQT_TQIO_ULONG maxlen)
{
  cleanError();
  if (d->status < connected || d->flags & passiveSocket)
    return -2;

  int retval;

  if ((d->flags & inputBufferedSocket) == 0)
    {
      // we aren't buffering this socket, so just pass along
      // the call to the real read method

      if (sockfd == -1)
	return -2;
      if (data)
	retval = KSocks::self()->read(sockfd, data, maxlen);
      else
	retval = skipData(sockfd, maxlen);
      if (retval == -1)
	setError(IO_ReadError, errno);
    }
  else
    {
      // this socket is being buffered. So read from the buffer

      // LOCK BUFFER MUTEX

      retval = consumeReadBuffer(maxlen, data);
      if (retval == 0)
	{
	  // consumeReadBuffer returns 0 only if the buffer is
	  // empty
	  if (sockfd == -1)
	    return 0;		// buffer is clear now, indicate EOF
	  setError(IO_ReadError, EWOULDBLOCK);
	  retval = -1;
	}

      // UNLOCK BUFFER MUTEX

    }
  return retval;
}

TQT_TQIO_LONG KExtendedSocket::tqwriteBlock(const char *data, TQT_TQIO_ULONG len)
{
  cleanError();
  if (d->status < connected || d->status >= closing || d->flags & passiveSocket)
    return -2;
  if (sockfd == -1)
    return -2;

  if (len == 0)
    return 0;			// what's to write?

  int retval;

  if ((d->flags & outputBufferedSocket) == 0)
    {
      // socket not buffered. Just call write
      retval = KSocks::self()->write(sockfd, data, len);
      if (retval == -1)
	setError(IO_WriteError, errno);
      else
	emit bytesWritten(retval);
    }
  else
    {
      // socket is buffered. Feed the write buffer

      // LOCK BUFFER MUTEX

      register unsigned wsize = writeBufferSize();
      if (d->outMaxSize == (int)wsize) // (int) to get rid of annoying warning
	{
	  // buffer is full!
	  setError(IO_WriteError, EWOULDBLOCK);
	  retval = -1;
	}
      else
	{
	  if (d->outMaxSize != -1 && wsize + len > (unsigned)d->outMaxSize)
	    // we cannot write all data. Write just as much as to fill the buffer
	    len = d->outMaxSize - wsize;

	  // len > 0 here
	  retval = feedWriteBuffer(len, data);
	  if (wsize == 0 || d->emitWrite)
	    // buffer was empty, which means that the notifier is probably disabled
	    d->qsnOut->setEnabled(true);
	}

      // UNLOCK BUFFER MUTEX
    }

  return retval;
}

int KExtendedSocket::peekBlock(char *data, uint maxlen)
{
  if (d->status < connected || d->flags & passiveSocket)
    return -2;
  if (sockfd == -1)
    return -2;

  // need to LOCK MUTEX around this call...

  if (d->flags & inputBufferedSocket)
    return consumeReadBuffer(maxlen, data, false);

  return 0;
}

int KExtendedSocket::unreadBlock(const char *, uint)
{
  // Always return -1, indicating this is not supported
  setError(IO_ReadError, ENOSYS);
  return -1;
}

#ifdef USE_QT3
int KExtendedSocket::bytesAvailable() const
#endif // USE_QT3
#ifdef USE_QT4
qint64 KExtendedSocket::bytesAvailable() const
#endif // USE_QT4
{
  if (d->status < connected || d->flags & passiveSocket)
    return -2;

  // as of now, we don't do any extra processing
  // we only work in input-buffered sockets
  if (d->flags & inputBufferedSocket)
    return KBufferedIO::bytesAvailable();

  return 0;			// TODO: FIONREAD ioctl
}

int KExtendedSocket::waitForMore(int msecs)
{
  cleanError();
  if (d->flags & passiveSocket || d->status < connected || d->status >= closing)
    return -2;
  if (sockfd == -1)
    return -2;

  fd_set rd;
  FD_ZERO(&rd);
  FD_SET(sockfd, &rd);
  timeval tv;
  tv.tv_sec = msecs / 1000;
  tv.tv_usec = (msecs % 1000) * 1000;

  int retval = KSocks::self()->select(sockfd + 1, &rd, NULL, NULL, &tv);
  if (retval == -1)
    {
      setError(IO_FatalError, errno);
      return -1;
    }
  else if (retval != 0)
    socketActivityRead();	// do read processing

  return bytesAvailable();
}

int KExtendedSocket::getch()
{
  unsigned char c;
  int retval;
  retval = tqreadBlock((char*)&c, sizeof(c));

  if (retval < 0)
    return retval;
  return c;
}

int KExtendedSocket::putch(int ch)
{
  unsigned char c = (char)ch;
  return tqwriteBlock((char*)&c, sizeof(c));
}

// sets the emission of the readyRead signal
void KExtendedSocket::enableRead(bool enable)
{
  // check if we can disable the socket notifier
  // saves us a few cycles
  // this is so because in buffering mode, we rely on these signals
  // being emitted to do our I/O. We couldn't disable them here
  if (!enable && (d->flags & inputBufferedSocket) == 0 && d->qsnIn)
    d->qsnIn->setEnabled(false);
  else if (enable && d->qsnIn)
    // we can enable it always
    d->qsnIn->setEnabled(true);
  d->emitRead = enable;
}

// sets the emission of the readyWrite signal
void KExtendedSocket::enableWrite(bool enable)
{
  // same thing as above
  if (!enable && (d->flags & outputBufferedSocket) == 0 && d->qsnOut)
    d->qsnOut->setEnabled(false);
  else if (enable && d->qsnOut)
    // we can enable it always
    d->qsnOut->setEnabled(true);
  d->emitWrite = enable;
}

// protected slot
// this is connected to d->qsnIn::activated(int)
void KExtendedSocket::socketActivityRead()
{
  if (d->flags & passiveSocket)
    {
      emit readyAccept();
      return;
    }
  if (d->status == connecting)
    {
      connectionEvent();
      return;
    }
  if (d->status != connected)
    return;

  // do we need to do I/O here?
  if (d->flags & inputBufferedSocket)
    {
      // aye. Do read from the socket and feed our buffer
      TQByteArray a;
      char buf[1024];
      int len, totalread = 0;

      // LOCK MUTEX

      unsigned cursize = readBufferSize();

      if (d->inMaxSize == -1 || cursize < (unsigned)d->inMaxSize)
	{
	  do
	    {
	      // check that we can read that many bytes
	      if (d->inMaxSize != -1 && d->inMaxSize - (cursize + totalread) < sizeof(buf))
		// no, that would overrun the buffer
		// note that this will also make us exit the loop
		len = d->inMaxSize - (cursize + totalread);
	      else
		len = sizeof(buf);

	      len = KSocks::self()->read(sockfd, buf, len);
	      if (len > 0)
		{
		  // normal read operation
		  a.resize(a.size() + len);
		  memcpy(a.data() + totalread, buf, len);
		  totalread += len;	// totalread == a.size() now
		}
	      else if (len == 0)
		{
		  // EOF condition here
		  ::close(sockfd);
		  sockfd = -1;	// we're closed
		  d->qsnIn->deleteLater();
		  delete d->qsnOut;
		  d->qsnIn = d->qsnOut = NULL;
		  d->status = done;
		  emit closed(involuntary |
			      (readBufferSize() ? availRead : 0) |
			      (writeBufferSize() ? dirtyWrite : 0));
		  return;
		}
	      else
		{
		  // error!
		  setError(IO_ReadError, errno);
		  return;
		}
	      // will loop only for normal read operations
	    }
	  while (len == sizeof(buf));

	  feedReadBuffer(a.size(), a.data());
	}

      // UNLOCK MUTEX
    }
  else
    {
      // No input buffering, but the notifier fired
      // That means that either there is data to be read or that the 
      // socket closed.

      // try to read one byte. If we can't, then the socket got closed

      char c;
      int len = KSocks::self()->recv(sockfd, &c, sizeof(c), MSG_PEEK);
      if (len == 0)
	{
	  // yes, it's an EOF condition
	  d->qsnIn->setEnabled(false);
	  ::close(sockfd);
	  sockfd = -1;
	  d->status = done;
	  emit closed(involuntary);
	  return;
	}
    }

  if (d->emitRead)
    emit readyRead();
}

void KExtendedSocket::socketActivityWrite()
{
  if (d->flags & passiveSocket)
    return;
  if (d->status == connecting)
    {
      connectionEvent();
      return;
    }
  if (d->status != connected && d->status != closing)
    return;

  flush();

  bool empty = writeBufferSize() == 0;

  if (d->emitWrite && empty)
    emit readyWrite();
  else if (!d->emitWrite)
    {
      // check if we can disable the notifier
      d->qsnOut->setEnabled(!empty); // leave it enabled only if we have more data to send
    }
  if (d->status == closing && empty)
    {
      // done sending the missing data!
      d->status = done;

      delete d->qsnOut;
      ::close(sockfd);

      d->qsnOut = NULL;
      sockfd = -1;
      emit closed(delayed | (readBufferSize() ? availRead : 0));
    }
}

// this function is called whenever we have a "connection event"
// that is, whenever our asynchronously connecting socket throws
// an event
void KExtendedSocket::connectionEvent()
{
  if (d->status != connecting)
    return;			// move along. There's nothing to see here

  KResolverResults remote = d->resRemote.results();
  if (remote.count() == 0)
    {
      // We have a problem! Abort?
      kdError(170) << "KExtendedSocket::connectionEvent() called but no data available!\n";
      return;
    }

  int errcode = 0;

  if (sockfd != -1)
    {
      // our socket has activity
      // find out what it was
      int retval;
      socklen_t len = sizeof(errcode);
      retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char*)&errcode, &len);

      if (retval == -1 || errcode != 0)
	{
	  // socket activity and there was error?
	  // that means the socket probably did not connect
	  if (d->qsnIn)
	    delete d->qsnIn;
	  if (d->qsnOut)
	    delete d->qsnOut;
	  ::close(sockfd);

	  sockfd = -1;
	  d->qsnIn = d->qsnOut = NULL;
	  d->current++;
	  setError(IO_ConnectError, errcode);
	}
      else
	{
	  // hmm, socket activity and there was no error?
	  // that means it connected
	  // YAY!
	  cleanError();
	  d->status = connected;
	  setBlockingMode(true);
	  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
	  setBufferSize(d->flags & inputBufferedSocket ? -1 : 0,
			d->flags & outputBufferedSocket ? -1 : 0);
	  emit connectionSuccess();
	  return;
	}
    }

  // ok, we have to try something here
  // and sockfd == -1
  KResolverResults local = d->resLocal.results();
  unsigned localidx = 0;
  for ( ; d->current < remote.count(); d->current++)
    {
      // same code as in connect()
      if (local.count() != 0)
	{
	  // scan bindres for a local resuls family
	  for (localidx = 0; localidx < local.count(); localidx++)
	    if (remote[d->current].family() == local[localidx].family())
	      break;

	  if (remote[d->current].family() != local[localidx].family())
	    {
	      // no matching families for this
	      continue;
	    }

	  errno = 0;
	  sockfd = ::socket(remote[d->current].family(), remote[d->current].socketType(),
			    remote[d->current].protocol());
	  setError(IO_ConnectError, errno);
	  errcode = errno;
	  if (sockfd == -1)
	    continue;		// cannot create this socket
          fcntl(sockfd, F_SETFD, FD_CLOEXEC);
	  if (d->addressReusable)
	    setAddressReusable(sockfd, true);
	  setIPv6Only(d->ipv6only);
	  cleanError();
	  if (KSocks::self()->bind(sockfd, local[localidx].address(), 
				   local[localidx].length()) == -1)
	    {
	      ::close(sockfd);
	      sockfd = -1;
	      continue;
	    }
	}
      else
	{
	  // no need to bind, just create
	  sockfd = ::socket(remote[d->current].family(), remote[d->current].socketType(),
			    remote[d->current].protocol());
	  if (sockfd == -1)
	    {
	      setError(IO_ConnectError, errno);
	      errcode = errno;
	      continue;
	    }
          fcntl(sockfd, F_SETFD, FD_CLOEXEC);
	  if (d->addressReusable)
	    setAddressReusable(sockfd, true);
	  setIPv6Only(d->ipv6only);
	  cleanError();
	}

      if (KSocks::self()->hasWorkingAsyncConnect())
        setBlockingMode(false);
      if (KSocks::self()->connect(sockfd, remote[d->current].address(), 
				  remote[d->current].length()) == -1)
	{
	  if (errno != EWOULDBLOCK && errno != EINPROGRESS)
	    {
	      setError(IO_ConnectError, errno);
	      ::close(sockfd);
	      sockfd = -1;
	      errcode = errno;
	      continue;
	    }

	  // error here is either EWOULDBLOCK or EINPROGRESS
	  // so, it is a good condition
	  d->qsnIn = new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
	  TQObject::connect(d->qsnIn, TQT_SIGNAL(activated(int)), this, TQT_SLOT(socketActivityRead()));
	  d->qsnOut = new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
	  TQObject::connect(d->qsnOut, TQT_SIGNAL(activated(int)), this, TQT_SLOT(socketActivityWrite()));

	  // ok, let the Qt event loop do the selecting for us
	  return;
	}

      // eh, what?
      // the non-blocking socket returned valid connection?
      // already?
      // I suppose that could happen...
      cleanError();
      d->status = connected;
      setBlockingMode(true);
      setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
      setBufferSize(d->flags & inputBufferedSocket ? -1 : 0,
		    d->flags & outputBufferedSocket ? -1 : 0);
      emit connectionSuccess();
      return;
    }

  // if we got here, it means that there are no more options to connect
  d->status = lookupDone;	// go back
  emit connectionFailed(errcode);
}

void KExtendedSocket::dnsResultsReady()
{
  // check that this function was called in a valid state
  if (d->status != lookupInProgress)
    return;

  // valid state. Are results fully ready?
  if (d->resRemote.isRunning() || d->resLocal.isRunning())
    // no, still waiting for answer in one of the lookups
    return;

  // ok, we have all results
  // count how many results we have
  int n = d->resRemote.results().count() + d->resLocal.results().count();

  if (n)
    {
      d->status = lookupDone;
      cleanError();
    }
  else
    {
      d->status = nothing;
      setError(IO_LookupError, KResolver::NoName);
    }

  emit lookupFinished(n);

  return;
}

void KExtendedSocket::startAsyncConnectSlot()
{
  TQObject::disconnect(this, TQT_SIGNAL(lookupFinished(int)), this, TQT_SLOT(startAsyncConnectSlot()));

  if (d->status == lookupDone)
    startAsyncConnect();
}

int KExtendedSocket::resolve(sockaddr *sock, ksocklen_t len, TQString &host,
			     TQString &port, int flags)
{
  kdDebug(170) << "Deprecated function called:" << k_funcinfo << endl;

  int err;
  char h[NI_MAXHOST], s[NI_MAXSERV];

  h[0] = s[0] = '\0';

  err = getnameinfo(sock, len, h, sizeof(h) - 1, s, sizeof(s) - 1, flags);
  host = TQString::fromUtf8(h);
  port = TQString::fromUtf8(s);

  return err;
}

int KExtendedSocket::resolve(::KSocketAddress *sock, TQString &host, TQString &port,
			     int flags)
{
  return resolve(sock->data, sock->datasize, host, port, flags);
}

TQPtrList<KAddressInfo> KExtendedSocket::lookup(const TQString& host, const TQString& port,
					    int userflags, int *error)
{
  kdDebug(170) << "Deprecated function called:" << k_funcinfo << endl;

  int socktype, familyMask, flags;
  unsigned i;
  TQPtrList<KAddressInfo> l;

  /* check socket type flags */
  if (!process_flags(userflags, socktype, familyMask, flags))
    return l;

//  kdDebug(170) << "Performing lookup on " << host << "|" << port << endl;
  KResolverResults res = KResolver::resolve(host, port, flags, familyMask);
  if (res.error())
    {
      if (error)
	*error = res.error();
      return l;
    }

  for (i = 0; i < res.count(); i++)
    {
      KAddressInfo *ai = new KAddressInfo();

      // I should have known that using addrinfo was going to come
      // and bite me back some day...
      ai->ai = (addrinfo *) malloc(sizeof(addrinfo));
      memset(ai->ai, 0, sizeof(addrinfo));

      ai->ai->ai_family = res[i].family();
      ai->ai->ai_socktype = res[i].socketType();
      ai->ai->ai_protocol = res[i].protocol();
      TQString canon = res[i].canonicalName();
      if (!canon.isEmpty())
	{
	  ai->ai->ai_canonname = (char *) malloc(canon.length()+1);
	  strcpy(ai->ai->ai_canonname, canon.ascii()); // ASCII here is intentional
	}
      if ((ai->ai->ai_addrlen = res[i].length()))
	{
	  ai->ai->ai_addr = (struct sockaddr *) malloc(res[i].length());
	  memcpy(ai->ai->ai_addr, res[i].address().address(), res[i].length());
	}
      else
	{
	  ai->ai->ai_addr = 0;
	}

      ai->addr = ::KSocketAddress::newAddress(ai->ai->ai_addr, ai->ai->ai_addrlen);

      l.append(ai);
    }

  if ( error )
      *error = 0;               // all is fine!

  return l;
}

::KSocketAddress *KExtendedSocket::localAddress(int fd)
{
  ::KSocketAddress *local;
  struct sockaddr static_sa, *sa = &static_sa;
  ksocklen_t len = sizeof(static_sa);

  /* find out the socket length, in advance
   * we use a sockaddr allocated on the heap just not to pass down
   * a NULL pointer to the first call. Some systems are reported to
   * set len to 0 if we pass NULL as the sockaddr */
  if (KSocks::self()->getsockname(fd, sa, &len) == -1)
    return NULL;		// error!

  /* was it enough? */
  if (len > sizeof(static_sa)
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
      || sa->sa_len > sizeof(static_sa)
#endif
      )
    {
      /* nope, malloc a new socket with the proper size */

#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
      if (sa->sa_len != len)
        len = sa->sa_len;
#endif

      sa = (sockaddr*)malloc(len);
      if (sa == NULL)
	return NULL;		// out of memory

      if (KSocks::self()->getsockname(fd, sa, &len) == -1)
	{
	  free(sa);
	  return NULL;
	}

      local = ::KSocketAddress::newAddress(sa, len);
      free(sa);
    }
  else
    local = ::KSocketAddress::newAddress(sa, len);

  return local;
}

/* This is exactly the same code as localAddress, except
 * we call getpeername here */
::KSocketAddress *KExtendedSocket::peerAddress(int fd)
{
  ::KSocketAddress *peer;
  struct sockaddr static_sa, *sa = &static_sa;
  ksocklen_t len = sizeof(static_sa);

  /* find out the socket length, in advance
   * we use a sockaddr allocated on the heap just not to pass down
   * a NULL pointer to the first call. Some systems are reported to
   * set len to 0 if we pass NULL as the sockaddr */
  if (KSocks::self()->getpeername(fd, sa, &len) == -1)
    return NULL;		// error!

  /* was it enough? */
  if (len > sizeof(static_sa)
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
      || sa->sa_len > sizeof(static_sa)
#endif
      )
    {
      /* nope, malloc a new socket with the proper size */

#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
      if (sa->sa_len != len)
        len = sa->sa_len;
#endif

      sa = (sockaddr*)malloc(len);
      if (sa == NULL)
	return NULL;		// out of memory

      if (KSocks::self()->getpeername(fd, sa, &len) == -1)
	{
	  free(sa);
	  return NULL;
	}

      peer = ::KSocketAddress::newAddress(sa, len);
      free(sa);
    }
  else
    peer = ::KSocketAddress::newAddress(sa, len);

  return peer;
}

TQString KExtendedSocket::strError(int code, int syserr)
{
  const char * msg;
  if (code == IO_LookupError)
    msg = gai_strerror(syserr);
  else
    msg = strerror(syserr);

  return TQString::fromLocal8Bit(msg);
}


TQSocketNotifier *KExtendedSocket::readNotifier() { return d->qsnIn; }
TQSocketNotifier *KExtendedSocket::writeNotifier() { return d->qsnOut; }

/*
 * class KAddressInfo
 */

#if 0
KAddressInfo::KAddressInfo(addrinfo *p)
{
   ai = (addrinfo *) malloc(sizeof(addrinfo));
   memcpy(ai, p, sizeof(addrinfo));
   ai->ai_next = NULL;
   if (p->ai_canonname)
   {
      ai->ai_canonname = (char *) malloc(strlen(p->ai_canonname)+1);
      strcpy(ai->ai_canonname, p->ai_canonname);
   }
   if (p->ai_addr && p->ai_addrlen)
   {
      ai->ai_addr = (struct sockaddr *) malloc(p->ai_addrlen);
      memcpy(ai->ai_addr, p->ai_addr, p->ai_addrlen);
   }
   else
   {
      ai->ai_addr = 0;
      ai->ai_addrlen = 0;
   }

   addr = ::KSocketAddress::newAddress(ai->ai_addr, ai->ai_addrlen);
}
#endif
KAddressInfo::~KAddressInfo()
{
  if (ai && ai->ai_canonname)
    free(ai->ai_canonname);

  if (ai && ai->ai_addr)
    free(ai->ai_addr);  

  if (ai)
    free(ai);
  delete addr;
}

int KAddressInfo::flags() const
{
  return ai->ai_flags;
}

int KAddressInfo::family() const
{
  return ai->ai_family;
}

int KAddressInfo::socktype() const
{
  return ai->ai_socktype;
}

int KAddressInfo::protocol() const
{
  return ai->ai_protocol;
}

const char* KAddressInfo::canonname() const
{
  return ai->ai_canonname;
}

void KExtendedSocket::virtual_hook( int id, void* data )
{ KBufferedIO::virtual_hook( id, data ); }

#include "kextsock.moc"