private static byte[] WritePackets(int encapsulatedPacketCount, int passthroughPacketCount, Action <Stream, int> action)
 {
     byte[] buffer = null;
     using (LimitedMemoryStream stream = new LimitedMemoryStream())
     {
         using (Stream tdsStream = CreateSslOverTdsStream(stream))
         {
             for (int index = 0; index < encapsulatedPacketCount; index++)
             {
                 action(tdsStream, index);
             }
             InvokeFinishHandshake(tdsStream);//tdsStream.FinishHandshake();
             for (int index = 0; index < passthroughPacketCount; index++)
             {
                 action(tdsStream, encapsulatedPacketCount + index);
             }
         }
         buffer = stream.ToArray();
     }
     return(buffer);
 }
 private static void ReadPackets(byte[] buffer, int encapsulatedPacketCount, int passthroughPacketCount, int maxPacketReadLength, byte[] output, Func <Stream, byte[], int, int, int> action)
 {
     using (LimitedMemoryStream stream = new LimitedMemoryStream(buffer, maxPacketReadLength))
         using (Stream tdsStream = CreateSslOverTdsStream(stream))
         {
             int    offset = 0;
             byte[] bytes  = new byte[TdsEnums.DEFAULT_LOGIN_PACKET_SIZE];
             for (int index = 0; index < encapsulatedPacketCount; index++)
             {
                 Array.Clear(bytes, 0, bytes.Length);
                 int packetBytes = ReadPacket(tdsStream, action, bytes);
                 Array.Copy(bytes, 0, output, offset, packetBytes);
                 offset += packetBytes;
             }
             InvokeFinishHandshake(tdsStream);
             for (int index = 0; index < passthroughPacketCount; index++)
             {
                 Array.Clear(bytes, 0, bytes.Length);
                 int packetBytes = ReadPacket(tdsStream, action, bytes);
                 Array.Copy(bytes, 0, output, offset, packetBytes);
                 offset += packetBytes;
             }
         }
 }