+ tTCPConnection *conn = Node->ImplPtr;
+ size_t rem = Length;
+
+ ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
+
+ // Check if connection is open
+ while( conn->State == TCP_ST_HALFOPEN || conn->State == TCP_ST_SYN_SENT )
+ Threads_Yield();
+ if( conn->State != TCP_ST_OPEN ) {
+ LEAVE('i', 0);
+ return 0;
+ }
+
+ while( rem > TCP_MAX_PACKET_SIZE )
+ {
+ TCP_INT_SendDataPacket(conn, TCP_MAX_PACKET_SIZE, Buffer);
+ Buffer += TCP_MAX_PACKET_SIZE;
+ }
+
+ TCP_INT_SendDataPacket(conn, rem, Buffer);
+
+ LEAVE('i', Length);
+ return Length;
+}
+
+/**
+ * \brief Open a connection to another host using TCP
+ * \param Conn Connection structure
+ */
+void TCP_StartConnection(tTCPConnection *Conn)
+{
+ tTCPHeader hdr;
+
+ Conn->State = TCP_ST_SYN_SENT;
+
+ hdr.SourcePort = htons(Conn->LocalPort);
+ hdr.DestPort = htons(Conn->RemotePort);
+ Conn->NextSequenceSend = rand();
+ hdr.SequenceNumber = htonl(Conn->NextSequenceSend);
+ hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4;
+ hdr.Flags = TCP_FLAG_SYN;
+ hdr.WindowSize = htons(TCP_WINDOW_SIZE); // Max
+ hdr.Checksum = 0; // TODO
+ hdr.UrgentPointer = 0;
+
+ TCP_SendPacket( Conn, sizeof(tTCPHeader), &hdr );
+
+ Conn->NextSequenceSend ++;
+ Conn->State = TCP_ST_SYN_SENT;
+ return ;