// File: lotuskip[P]yuxtapa/client/network.cpp
// Function: Network::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool Network::connect(string &errors)
{
	// determine ip and port
	string sip = Config::get_server_ip(); // this is just what was read from the config; could be garbage!
	string port = "", ip = "";
	size_t i;
	if((i = sip.rfind(':')) != string::npos && i > 1 && i != sip.size()-1)
	{
		port = sip.substr(i+1);
		ip = sip.substr(0, i);
		if(ip[0] == '[') // this would indicate IPv6
		{
			ip.erase(0,1);
			if(ip.empty())
			{
				errors = "Not a valid address: " + sip;
				return false;
			}
			ip.erase(ip.size()-1,1); // erase assumed ']'
		}
	}
	else
	{
		errors = "Not a valid address: " + sip;
		return false;
	}

	// create the socket
	struct addrinfo hints;
	int rv;
	bzero(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	// get server info:
	if((rv = getaddrinfo(ip.c_str(), port.c_str(), &hints, &servinfo)))
	{
		errors = string("Error in getaddrinfo: ") + gai_strerror(rv);
		return false;
	}
	// initialize the_serv and make the socket based on its protocol info:
	for(the_serv = servinfo; the_serv; the_serv = the_serv->ai_next)
	{
		if((s_me = socket(the_serv->ai_family, the_serv->ai_socktype, the_serv->ai_protocol)) != -1)
			break;
	}
	if(!the_serv)
	{
		errors = "Failed to create socket!";
		freeaddrinfo(servinfo);
		return false;
	}
	// set socket to nonblock mode:
	fcntl(s_me, F_SETFL, O_NONBLOCK);

	last_sent_axn.update(); // must init this at some point

	add_msg("[Connecting to " + sip + "...]", 15);
	// construct a hello message:
	send_buffer.add((unsigned char)MID_HELLO);
	send_buffer.add(INTR_VERSION);
	add_id_info_for_server(sip);
	send_buffer.add(Config::get_nick());
	
	// send hello message (repeatedly) and wait for a reply
	int k;
	for(char j = 0; j < CONNECTION_ATTEMPTS; ++j)
	{
		if(do_send())
		{
			errors = "Problem sending connection request.";
			close(s_me);
			freeaddrinfo(servinfo);
			return false;
		}
		// Wait for the reply (the timeout on the getch() call will delay):
		for(k = 0; k < REPLY_WAIT_MS/GETCH_TIMEOUT; ++k)
		{
			if(do_receive() != -1)
			{
				// got *some* message
				unsigned char mid = recv_buffer.read_ch();
				switch(mid)
				{
				case MID_HELLO:
					Base::print_str("(identified)", 7, 0, 5, STAT_WIN, false);
					return true;
				case MID_HELLO_FULL:
				case MID_HELLO_BANNED:
				case MID_HELLO_VERSION:
				case MID_HELLO_STEAL:
					errors = hello_errors[mid - MID_HELLO_FULL];
					close(s_me);
					freeaddrinfo(servinfo);
					return false;
				case MID_HELLO_NEWID:
				{
					cur_id = recv_buffer.read_sh();
					string passw;
					recv_buffer.read_str(passw);
					store_id_for_server(cur_id, sip, passw);
					Base::print_str("(new id)", 7, 0, 5, STAT_WIN, false);
					return true;
				}
				default: break; // ignore other messages
				}
			} // received something
			if(getch() == KEYCODE_INT)
			{
				errors = "Aborted by user.";
				return false;
			}
		} // listen for connections loop
	} // send connection requests loop
	errors = "Did not get a reply from the server.";
	close(s_me);
	freeaddrinfo(servinfo);
	return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x10(%rbp)
callq 0x16150
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x5220
leaq -0x51(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x5840
movq -0x210(%rbp), %rdx
leaq 0x983b(%rip), %rsi      # 0x1c489
leaq -0x50(%rbp), %rdi
callq 0xa030
jmp 0x12c59
leaq -0x51(%rbp), %rdi
callq 0x5540
leaq -0x89(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x5840
movq -0x218(%rbp), %rdx
leaq 0x9806(%rip), %rsi      # 0x1c489
leaq -0x88(%rbp), %rdi
callq 0xa030
jmp 0x12c91
leaq -0x89(%rbp), %rdi
callq 0x5540
leaq -0x30(%rbp), %rdi
movl $0x3a, %esi
movq $-0x1, %rdx
callq 0x5910
movq %rax, -0x98(%rbp)
cmpq $-0x1, %rax
je 0x12ea7
cmpq $0x1, -0x98(%rbp)
jbe 0x12ea7
movq -0x98(%rbp), %rax
movq %rax, -0x220(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x5350
movq %rax, %rcx
movq -0x220(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0x12ea7
movq -0x98(%rbp), %rdx
incq %rdx
leaq -0xb8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movq $-0x1, %rcx
callq 0x54e0
jmp 0x12d22
leaq -0x50(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
movq %rsi, -0x228(%rbp)
callq 0x5600
movq -0x228(%rbp), %rdi
callq 0x5968
movq -0x98(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0xd8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x54e0
jmp 0x12d62
leaq -0x88(%rbp), %rdi
movq %rdi, -0x238(%rbp)
leaq -0xd8(%rbp), %rsi
movq %rsi, -0x240(%rbp)
callq 0x5600
movq -0x240(%rbp), %rdi
callq 0x5968
movq -0x238(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5930
movq %rax, -0x230(%rbp)
jmp 0x12da8
movq -0x230(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5b, %eax
jne 0x12ea5
xorl %eax, %eax
movl %eax, %esi
leaq -0x88(%rbp), %rdi
movl $0x1, %edx
callq 0x5120
jmp 0x12dd2
leaq -0x88(%rbp), %rdi
callq 0x57f0
testb $0x1, %al
jne 0x12de7
jmp 0x12e77
leaq 0x9942(%rip), %rsi      # 0x1c730
leaq -0xf8(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x14570
jmp 0x12e00
movq -0x10(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x5600
leaq -0xf8(%rbp), %rdi
callq 0x5968
movb $0x0, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0x134d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x5540
jmp 0x13516
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x89(%rbp), %rdi
callq 0x5540
jmp 0x1350d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x13501
leaq -0x88(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x5350
movq -0x248(%rbp), %rdi
movq %rax, %rsi
decq %rsi
movl $0x1, %edx
callq 0x5120
jmp 0x12ea3
jmp 0x12ea5
jmp 0x12eef
leaq 0x9882(%rip), %rsi      # 0x1c730
leaq -0x120(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x14570
jmp 0x12ec0
movq -0x10(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x5600
leaq -0x120(%rbp), %rdi
callq 0x5968
movb $0x0, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0x134d5
xorps %xmm0, %xmm0
movaps %xmm0, -0x130(%rbp)
movaps %xmm0, -0x140(%rbp)
movaps %xmm0, -0x150(%rbp)
movl $0x0, -0x14c(%rbp)
movl $0x2, -0x148(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x5200
movq %rax, -0x258(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x5200
movq -0x258(%rbp), %rdi
movq %rax, %rsi
leaq 0x17448(%rip), %rcx     # 0x2a390
leaq -0x150(%rbp), %rdx
callq 0x5630
movl %eax, -0x24c(%rbp)
jmp 0x12f5c
movl -0x24c(%rbp), %eax
movl %eax, -0x154(%rbp)
cmpl $0x0, %eax
je 0x13041
leaq -0x199(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x5840
movq -0x260(%rbp), %rdx
leaq 0x97b4(%rip), %rsi      # 0x1c746
leaq -0x198(%rbp), %rdi
callq 0xa030
jmp 0x12fa0
movl -0x154(%rbp), %edi
callq 0x5700
movq %rax, %rdx
leaq -0x178(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0xa1d0
jmp 0x12fc3
movq -0x10(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x5600
leaq -0x178(%rbp), %rdi
callq 0x5968
leaq -0x198(%rbp), %rdi
callq 0x5968
leaq -0x199(%rbp), %rdi
callq 0x5540
movb $0x0, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0x134d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x13030
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x5968
leaq -0x199(%rbp), %rdi
callq 0x5540
jmp 0x13501
movq 0x17348(%rip), %rax     # 0x2a390
movq %rax, 0x17349(%rip)     # 0x2a398
cmpq $0x0, 0x17341(%rip)     # 0x2a398
je 0x1309f
movq 0x17338(%rip), %rax     # 0x2a398
movl 0x4(%rax), %edi
movq 0x1732e(%rip), %rax     # 0x2a398
movl 0x8(%rax), %esi
movq 0x17324(%rip), %rax     # 0x2a398
movl 0xc(%rax), %edx
callq 0x5130
movl %eax, 0x1731e(%rip)     # 0x2a3a0
cmpl $-0x1, %eax
je 0x13089
jmp 0x1309f
jmp 0x1308b
movq 0x17306(%rip), %rax     # 0x2a398
movq 0x28(%rax), %rax
movq %rax, 0x172fb(%rip)     # 0x2a398
jmp 0x1304f
cmpq $0x0, 0x172f1(%rip)     # 0x2a398
jne 0x130da
movq -0x10(%rbp), %rdi
leaq 0x96a9(%rip), %rsi      # 0x1c75d
callq 0x56d0
jmp 0x130bb
movq 0x172ce(%rip), %rdi     # 0x2a390
callq 0x56a0
movb $0x0, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0x134d5
movl 0x172c0(%rip), %edi     # 0x2a3a0
xorl %eax, %eax
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x52b0
jmp 0x130f3
leaq 0x172ae(%rip), %rdi     # 0x2a3a8
callq 0x19b60
jmp 0x13101
leaq 0x966e(%rip), %rsi      # 0x1c776
leaq -0x1e0(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x14570
jmp 0x1311a
leaq 0x9665(%rip), %rdx      # 0x1c786
leaq -0x1c0(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0xa1d0
jmp 0x13136
leaq -0x1c0(%rbp), %rdi
movl $0xf, %esi
callq 0xe180
jmp 0x13149
leaq -0x1c0(%rbp), %rdi
callq 0x5968
leaq -0x1e0(%rbp), %rdi
callq 0x5968
leaq 0x16970(%rip), %rdi     # 0x29ad8
xorl %esi, %esi
callq 0x19c90
jmp 0x13171
leaq 0x16960(%rip), %rdi     # 0x29ad8
movl $0x3, %esi
callq 0x19cd0
jmp 0x13184
leaq -0x30(%rbp), %rdi
callq 0x13530
jmp 0x1318f
callq 0x16160
movq %rax, -0x268(%rbp)
jmp 0x1319d
movq -0x268(%rbp), %rsi
leaq 0x1692d(%rip), %rdi     # 0x29ad8
callq 0x19d40
jmp 0x131b2
movb $0x0, -0x1e5(%rbp)
movsbl -0x1e5(%rbp), %eax
cmpl $0x3, %eax
jge 0x1349c
callq 0x13720
movb %al, -0x269(%rbp)
jmp 0x131d6
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x131e2
jmp 0x13257
movq -0x10(%rbp), %rdi
leaq 0x959e(%rip), %rsi      # 0x1c78b
callq 0x56d0
jmp 0x131f4
movl 0x171a6(%rip), %edi     # 0x2a3a0
callq 0x5900
jmp 0x13201
movq 0x17188(%rip), %rdi     # 0x2a390
callq 0x56a0
movb $0x0, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0x134d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x13246
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x5968
leaq -0x1e0(%rbp), %rdi
callq 0x5968
jmp 0x13501
movl $0x0, -0x1e4(%rbp)
cmpl $0x32, -0x1e4(%rbp)
jge 0x13487
callq 0x13790
movw %ax, -0x26c(%rbp)
jmp 0x1327c
movw -0x26c(%rbp), %ax
cwtl
cmpl $-0x1, %eax
je 0x1342d
leaq 0x16034(%rip), %rdi     # 0x292c8
callq 0x19ea0
movb %al, -0x26d(%rbp)
jmp 0x132a1
movb -0x26d(%rbp), %al
movb %al, -0x1e6(%rbp)
movzbl -0x1e6(%rbp), %eax
movl %eax, -0x274(%rbp)
testl %eax, %eax
je 0x132e4
jmp 0x132c0
movl -0x274(%rbp), %eax
addl $-0x10, %eax
subl $0x4, %eax
jb 0x13318
jmp 0x132d0
movl -0x274(%rbp), %eax
subl $0x14, %eax
je 0x13368
jmp 0x13429
leaq 0x94c4(%rip), %rdi      # 0x1c7af
movl $0x7, %esi
movl $0x5, %ecx
xorl %r9d, %r9d
movl %r9d, %edx
movl %r9d, %r8d
callq 0x9480
jmp 0x13305
movb $0x1, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0x134d5
movzbl -0x1e6(%rbp), %eax
shll $0x5, %eax
leaq 0x16fe7(%rip), %rcx     # 0x2a310
leaq -0x200(%rax,%rcx), %rsi
movq -0x10(%rbp), %rdi
callq 0x5060
jmp 0x1333c
movl 0x1705e(%rip), %edi     # 0x2a3a0
callq 0x5900
jmp 0x13349
movq 0x17040(%rip), %rdi     # 0x2a390
callq 0x56a0
movb $0x0, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0x134d5
leaq 0x15f59(%rip), %rdi     # 0x292c8
callq 0x19e60
movw %ax, -0x276(%rbp)
jmp 0x1337d
movw -0x276(%rbp), %ax
movw %ax, 0x1702d(%rip)      # 0x2a3b8
leaq -0x208(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0x56f0
movq -0x280(%rbp), %rsi
leaq 0x15f1c(%rip), %rdi     # 0x292c8
callq 0x19ed0
jmp 0x133b3
movzwl 0x16ffe(%rip), %edi     # 0x2a3b8
leaq -0x30(%rbp), %rsi
leaq -0x208(%rbp), %rdx
callq 0x137e0
jmp 0x133cc
leaq 0x93e9(%rip), %rdi      # 0x1c7bc
movl $0x7, %esi
movl $0x5, %ecx
xorl %r9d, %r9d
movl %r9d, %edx
movl %r9d, %r8d
callq 0x9480
jmp 0x133ed
movb $0x1, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x5968
jmp 0x134d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x5968
jmp 0x13501
jmp 0x1342b
jmp 0x1342d
movq 0x14b84(%rip), %rax     # 0x27fb8
movq (%rax), %rdi
callq 0x5660
movl %eax, -0x284(%rbp)
jmp 0x13444
movl -0x284(%rbp), %eax
cmpl $0x3, %eax
jne 0x13471
movq -0x10(%rbp), %rdi
leaq 0x936b(%rip), %rsi      # 0x1c7c5
callq 0x56d0
jmp 0x13461
movb $0x0, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0x134d5
jmp 0x13473
movl -0x1e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e4(%rbp)
jmp 0x13261
jmp 0x13489
movb -0x1e5(%rbp), %al
addb $0x1, %al
movb %al, -0x1e5(%rbp)
jmp 0x131b9
movq -0x10(%rbp), %rdi
leaq 0x932f(%rip), %rsi      # 0x1c7d6
callq 0x56d0
jmp 0x134ae
movl 0x16eec(%rip), %edi     # 0x2a3a0
callq 0x5900
jmp 0x134bb
movq 0x16ece(%rip), %rdi     # 0x2a390
callq 0x56a0
movb $0x0, -0x1(%rbp)
movl $0x1, -0xfc(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x5968
leaq -0x50(%rbp), %rdi
callq 0x5968
leaq -0x30(%rbp), %rdi
callq 0x5968
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
leaq -0x88(%rbp), %rdi
callq 0x5968
leaq -0x50(%rbp), %rdi
callq 0x5968
leaq -0x30(%rbp), %rdi
callq 0x5968
movq -0x60(%rbp), %rdi
callq 0x5830
nopl (%rax,%rax)