Uknown Record Type with office365

I’m working on an SMTPS feature using an ST microcontroller and freeRTOS with lwIP and mbedTLS. The email server I’m using is office365. I’ve got it working in the past reliably as far as I’m aware, but recently it’s only successfully completing the handshake 1 in 10 times if that. If the handshake succeeds, the email is properly sent. I’ve tracked down where the failure is occurring using the debugging feature set to 4 and recording the output into a file. The input record type during a stage of the server hello is expected to by 22 I believe, but I’m receiving 50, causing my handshake to fail. Below is my debug output, any ideas what might be causing this frequent failure?

0x20006a28: set_timer to 0 ms
0x20006a28: => handshake
0x20006a28: client state: 0
0x20006a28: => flush output
0x20006a28: <= flush output
0x20006a28: client state: 1
0x20006a28: => flush output
0x20006a28: <= flush output
0x20006a28: => write client hello
0x20006a28: client hello, max version: [3:3]
0x20006a28: dumping ‘client hello, random bytes’ (32 bytes)
0x20006a28: 0000: 87 57 bb 57 7f 64 a9 45 48 b7 ba 13 92 86 b7 5f .W.W.d.EH…_
0x20006a28: 0010: 10 8d e3 e7 05 99 45 e5 d3 c3 19 36 39 99 73 f4 …E…69.s.
0x20006a28: client hello, session id len.: 0
0x20006a28: dumping ‘client hello, session id’ (0 bytes)
0x20006a28: client hello, add ciphersuite: c02c
0x20006a28: client hello, add ciphersuite: c02b
0x20006a28: client hello, add ciphersuite: c030
0x20006a28: client hello, add ciphersuite: c02f
0x20006a28: client hello, add ciphersuite: 0035
0x20006a28: client hello, got 5 ciphersuites (excluding SCSVs)
0x20006a28: adding EMPTY_RENEGOTIATION_INFO_SCSV
0x20006a28: client hello, compress len.: 1
0x20006a28: client hello, compress alg.: 0
0x20006a28: client hello, adding server name extension: smtp.office365.com
0x20006a28: client hello, adding signature_algorithms extension
0x20006a28: client hello, adding supported_elliptic_curves extension
0x20006a28: client hello, adding supported_point_formats extension
0x20006a28: client hello, adding encrypt_then_mac extension
0x20006a28: client hello, adding extended_master_secret extension
0x20006a28: client hello, adding session ticket extension
0x20006a28: client hello, total extension length: 81
0x20006a28: => write handshake message
0x20006a28: => write record
0x20006a28: output record: msgtype = 22, version = [3:3], msglen = 138
0x20006a28: dumping ‘output record sent to network’ (143 bytes)
0x20006a28: 0000: 16 03 03 00 8a 01 00 00 86 03 03 87 57 bb 57 7f …W.W.
0x20006a28: 0010: 64 a9 45 48 b7 ba 13 92 86 b7 5f 10 8d e3 e7 05 d.EH…_…
0x20006a28: 0020: 99 45 e5 d3 c3 19 36 39 99 73 f4 00 00 0c c0 2c .E…69.s…,
0x20006a28: 0030: c0 2b c0 30 c0 2f 00 35 00 ff 01 00 00 51 00 00 .+.0./.5…Q…
0x20006a28: 0040: 00 17 00 15 00 00 12 73 6d 74 70 2e 6f 66 66 69 …smtp.offi
0x20006a28: 0050: 63 65 33 36 35 2e 63 6f 6d 00 0d 00 16 00 14 06 ce365.com
0x20006a28: 0060: 03 06 01 05 03 05 01 04 03 04 01 03 03 03 01 02 …
0x20006a28: 0070: 03 02 01 00 0a 00 06 00 04 00 18 00 17 00 0b 00 …
0x20006a28: 0080: 02 01 00 00 16 00 00 00 17 00 00 00 23 00 00 …#…
0x20006a28: => flush output
0x20006a28: message length: 143, out_left: 143
0x20006a28: ssl->f_send() returned 143 (-0xffffff71)
0x20006a28: <= flush output
0x20006a28: <= write record
0x20006a28: <= write handshake message
0x20006a28: <= write client hello
0x20006a28: client state: 2
0x20006a28: => flush output
0x20006a28: <= flush output
0x20006a28: => parse server hello
0x20006a28: => read record
0x20006a28: => fetch input
0x20006a28: in_left: 0, nb_want: 5
0x20006a28: in_left: 0, nb_want: 5
0x20006a28: ssl->f_recv(_timeout)() returned 5 (-0xfffffffb)
0x20006a28: <= fetch input
0x20006a28: dumping ‘input record header’ (5 bytes)
0x20006a28: 0000: 32 32 30 20 32 220 2
0x20006a28: input record: msgtype = 50, version = [50:48], msglen = 8242
0x20006a28: unknown record type
0x20006a28: => send alert message
0x20006a28: send alert level=2 message=10
0x20006a28: => write record
0x20006a28: output record: msgtype = 21, version = [3:3], msglen = 2
0x20006a28: dumping ‘output record sent to network’ (7 bytes)
0x20006a28: 0000: 15 03 03 00 02 02 0a …
0x20006a28: => flush output
0x20006a28: message length: 7, out_left: 7
0x20006a28: ssl->f_send() returned 7 (-0xfffffff9)
0x20006a28: <= flush output
0x20006a28: <= write record
0x20006a28: <= send alert message
0x20006a28: ssl_get_next_record() returned -29184 (-0x7200)
0x20006a28: mbedtls_ssl_read_record() returned -29184 (-0x7200)
0x20006a28: <= handshake