<commit_before>#include "Socket.h"
#include "Misc.h"
#include <utility>
#include <stdexcept>
#include <memory>

#ifdef WINDOWS
	#define WIN32_LEAN_AND_MEAN
	#define FD_SETSIZE 1024
	#include <winsock2.h>
	#include <ws2tcpip.h>
	#include <Windows.h>

	static bool winsockInitialized = false;
#elif defined POSIX
	#include <sys/select.h>
	#include <sys/socket.h>
	#include <sys/types.h>
	#include <netinet/in.h>
	#include <unistd.h>
	#include <arpa/inet.h>
	#include <netdb.h>
	#include <stdio.h>
	#include <string.h>
	#include <endian.h>
#endif

using namespace Utilities::Net;
using namespace std;

#ifdef WINDOWS
uintptr prepareRawSocket(Socket::Families family, Socket::Types type, string address, string port, bool isListener, addrinfo** addressInfo) {
#elif defined POSIX
int prepareRawSocket(Socket::Families family, Socket::Types type, string address, string port, bool isListener, addrinfo** addressInfo) {
#endif
	addrinfo serverHints;
	addrinfo* serverAddrInfo;

#ifdef WINDOWS
	if (!::winsockInitialized) {
		WSADATA startupData;
		::WSAStartup(514, &startupData);
		::winsockInitialized = true;
	}
	uintptr rawSocket;
#elif defined POSIX
	int rawSocket;
#endif

	memset(&serverHints, 0, sizeof(addrinfo));

	switch (family) {
		case Socket::Families::IPV4: serverHints.ai_family = AF_INET; break;
		case Socket::Families::IPV6: serverHints.ai_family = AF_INET6; break;
		#ifdef WINDOWS
		case Socket::Families::IPAny: serverHints.ai_family = AF_INET6; break;
		#elif defined POSIX
		case Socket::Families::IPAny: serverHints.ai_family = AF_UNSPEC; break;
		#endif
		default: throw runtime_error("Invalid Socket Family.");
	}

	switch (type) {
		case Socket::Types::TCP: serverHints.ai_socktype = SOCK_STREAM; break;
		default: throw runtime_error("Invalid Socket Type.");
	}

	if (isListener)
		serverHints.ai_flags = AI_PASSIVE;
		
	if (::getaddrinfo(address != "" ? address.c_str() : nullptr, port.c_str(), &serverHints, &serverAddrInfo) != 0 || serverAddrInfo == nullptr)
		throw runtime_error("Unable to get socket address information.");

	rawSocket = ::socket(serverAddrInfo->ai_family, serverAddrInfo->ai_socktype, serverAddrInfo->ai_protocol);
#ifdef WINDOWS
	if (rawSocket == INVALID_SOCKET) {
		::closesocket(rawSocket);
		::freeaddrinfo(serverAddrInfo);
		throw runtime_error("Unable to create socket.");
	}
	else {
		int opt = 0;
		::setsockopt(rawSocket, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char*>(&opt), sizeof(opt));
	}
#elif defined POSIX
	if (this->rawSocket == -1) {
		::close(this->rawSocket);
		::freeaddrinfo(serverAddrInfo);
		throw runtime_error("Unable to create socket.");
	}
#endif

	*addressInfo = serverAddrInfo;

	return rawSocket;
}

Socket::Socket(Families family, Types type) {<commit_msg>Fixed posix again.<commit_after>#include "Socket.h"
#include "Misc.h"
#include <utility>
#include <stdexcept>
#include <exception>
#include <memory>

#ifdef WINDOWS
	#define WIN32_LEAN_AND_MEAN
	#define FD_SETSIZE 1024
	#include <winsock2.h>
	#include <ws2tcpip.h>
	#include <Windows.h>

	static bool winsockInitialized = false;
#elif defined POSIX
	#include <sys/select.h>
	#include <sys/socket.h>
	#include <sys/types.h>
	#include <netinet/in.h>
	#include <unistd.h>
	#include <arpa/inet.h>
	#include <netdb.h>
	#include <stdio.h>
	#include <string.h>
	#include <endian.h>
#endif

using namespace Utilities::Net;
using namespace std;

#ifdef WINDOWS
uintptr prepareRawSocket(Socket::Families family, Socket::Types type, string address, string port, bool isListener, addrinfo** addressInfo) {
#elif defined POSIX
int prepareRawSocket(Socket::Families family, Socket::Types type, string address, string port, bool isListener, addrinfo** addressInfo) {
#endif
	addrinfo serverHints;
	addrinfo* serverAddrInfo;

#ifdef WINDOWS
	if (!::winsockInitialized) {
		WSADATA startupData;
		::WSAStartup(514, &startupData);
		::winsockInitialized = true;
	}
	uintptr rawSocket;
#elif defined POSIX
	int rawSocket;
#endif

	memset(&serverHints, 0, sizeof(addrinfo));

	switch (family) {
		case Socket::Families::IPV4: serverHints.ai_family = AF_INET; break;
		case Socket::Families::IPV6: serverHints.ai_family = AF_INET6; break;
		#ifdef WINDOWS
		case Socket::Families::IPAny: serverHints.ai_family = AF_INET6; break;
		#elif defined POSIX
		case Socket::Families::IPAny: serverHints.ai_family = AF_UNSPEC; break;
		#endif
		default: throw runtime_error("Invalid Socket Family.");
	}

	switch (type) {
		case Socket::Types::TCP: serverHints.ai_socktype = SOCK_STREAM; break;
		default: throw runtime_error("Invalid Socket Type.");
	}

	if (isListener)
		serverHints.ai_flags = AI_PASSIVE;
		
	if (::getaddrinfo(address != "" ? address.c_str() : nullptr, port.c_str(), &serverHints, &serverAddrInfo) != 0 || serverAddrInfo == nullptr)
		throw runtime_error("Unable to get socket address information.");

	rawSocket = ::socket(serverAddrInfo->ai_family, serverAddrInfo->ai_socktype, serverAddrInfo->ai_protocol);
#ifdef WINDOWS
	if (rawSocket == INVALID_SOCKET) {
		::closesocket(rawSocket);
		::freeaddrinfo(serverAddrInfo);
		throw runtime_error("Unable to create socket.");
	}
	else {
		int opt = 0;
		::setsockopt(rawSocket, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char*>(&opt), sizeof(opt));
	}
#elif defined POSIX
	if (rawSocket == -1) {
		::close(rawSocket);
		::freeaddrinfo(serverAddrInfo);
		throw runtime_error("Unable to create socket.");
	}
#endif

	*addressInfo = serverAddrInfo;

	return rawSocket;
}

Socket::Socket(Families family, Types type) {
{'fraction_non_alphanumeric': 0.13178012377138698, 'fraction_numerical': 0.008008736803785948, 'mean_word_length': 5.817617866004963, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1263160', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1931, 'n_words': 458}